You can find solutions here:
- In procedural_solutions.rb are our first attempts at writing Ruby. Almost everything is written with
whileandif - In good_solutions.rb we do a rekate on everything with our knowledge of
Enumerable
In a file called warmup.rb, solve the following problems:
Implement the factorial function fact(n).
- Implement it using a simple loop
- Implement it using recursion
Because Fibonacci is way too trivial, implement the following functions that work with Lucas series:
nth_lucas(n)-> returns the nth Lucas numberfirst_lucas(n)-> returns a list of the firstnLucas numbers
Quick hint:
L(0) = 2
L(1) = 1
L(n) = L(n-1) + L(n-2)
Those are classic problems for using module division:
- Given an integer
n, return the number of digits inn->count_digits(n) - Given an integer
n, return the sum of all digits inn->sum_digits(n) factorial_digits(n)-> for example, if we have145, we need to calculate1! + 4! + 5!
Implement a function, called fib_number(n), which takes an integer n and returns a number, which is formed by concatenating the first n Fibonacci numbers.
Examples:
fib_number 3 == 112
fib_number 10 == 11235813213455A hack number is an integer, that matches the following criteria:
- The number, represented in binary, is a palindrome
- The number, represented in binary, has an odd number of 1's in it
Example of hack numbers:
- 1 is
1in binary - 7 is
111in binary - 7919 is
1111011101111in binary
Implement the following functions:
hack?(n)-> checks ifnis a hack numbernext_hack(n)-> returns the next hack number, that is bigger thann
Few examples:
hack? 1 == true
next_hack 0 == 1
hack? 21 == true
next_hack 10 == 21
hack? 8191 == true
next_hack 8031 == 8191Implement a function, called count_vowels(str), which returns the count of all vowels in the string str.
Count uppercase vowels aswell!
The English vowels are aeiouy.
Examples:
count_vowels "Python" == 2
count_vowels "Theistareykjarbunga" == 8
count_vowels "grrrrgh!" == 0
count_vowels "Github is the second best thing that happend to programmers, after the keyboard!" == 22
count_vowels "A nice day to code!" == 8Implement a function, called count_consonants str), which returns the count of all consonants in the string str.
Count uppercase consonants as well!
The English consonants are bcdfghjklmnpqrstvwxz.
Examples:
count_consonants "Python" == 4
count_consonants "Theistareykjarbunga" == 11
count_consonants "grrrrgh!" == 7
count_consonants "Github is the second best thing that happend to programmers, after the keyboard!" == 44
count_consonants "A nice day to code!" == 6We denote the "Palindrome score" of an integer by the following function:
P(n) = 1, ifnis palindromeP(n) = 1 + P(s)wheresis the sum ofnand thereverse of n
Implement a function, called p_score(n), which finds the palindrome score for n.
Lets see two examples:
p_score(121) = 1, because121is a palindrome.p_score(48) = 3, because:
P(48) = 1 + P(48 + 84) = 132P(132) = 1 + 1 + P(132 + 321 = 363)P(363) = 1.- When we return from the recursion, we get 3.
Examples:
p_score 121 == 1
p_score 48 == 3
p_score 198 == 6A palindrome is a word, phrase, number, or other sequence of characters which reads the same backward or forward.
Implement the following function: palindrome?(obj) that checks if the given obj is palindrome.
Examples:
palindrome? 121 == true
palindrome? 123 == false
palindrome? 'kapak' == true
palindrome? 'baba' == false
palindrome? 'azobi4amma4iboza' == trueThe usual deal. Implement the following functions:
- Check if number is prime ->
prime?(n) - List the first
nprime numbers ->first_primes(n) - OPTIONAL: List the first
nprime numbers, using the Sieve of Eratosthenes
You are given a string, where there can be numbers. Return the sum of all numbers in that string (not digits, numbers)
Examples:
sum_of_numbers_in_string "1111" == 1111
sum_of_numbers_in_string "1abc33xyz22" == 1 + 33 + 22 = 56
sum_of_numbers_in_string "abcd" == 0Here is the explanation of what an Anagram is - http://en.wikipedia.org/wiki/Anagram
Implemenent the following functions:
anagrams?(a, b)- returns true, if the stringais an anagram ofb
A number is called balanced, if we take the middle of it and the sums of the left and right parts are equal.
For example, the number 1238033 is balanced, because it's left part is 123 and right part is 033.
We have : 1 + 2 + 3 = 0 + 3 + 3 = 6.
A number with only one digit is always balanced!
Implement the function balanced?(n) that checks if n is balanced.
Few examples:
balanced?(9) == True
balanced?(11) == True
balanced?(13) == False
balanced?(121) == True
balanced?(4518) == True
balanced?(28471) == False
balanced?(1238033) == TrueGiven an integer, implement the function zero_insert(n), which returns a new integer, constructed by the following algorithm:
- If two neighboring digits are the same (like
55), insert a 0 between them (505) - Also, if we add two neighboring digits and take their module by 10 (
% 10) and the result is 0 - add 0 between them.
For example, if we have the number 116457, result will be: 10160457:
- 1 and 1 are the same, so we insert 0 between them
6 + 4 % 10 = 0, so we insert 0 between them.
Few examples:
zero_insert(116457) == 10160457
zero_insert(55555555) == 505050505050505
zero_insert(1) == 1
zero_insert(6446) == 6040406