Created
May 10, 2015 00:28
-
-
Save MaggieMoss/c22c6093de4f0ea86928 to your computer and use it in GitHub Desktop.
Answers to common programming problems
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
problem one | |
write three functions that compute the sum of the numbers in a given list | |
using a: | |
for loop | |
def sum(num1, num2, num3) | |
numbers = [num1, num2, num3] | |
total_sum = 0 | |
for num in numbers | |
total_sum += num | |
end | |
return total_sum | |
end | |
print "\n" | |
print sum(1, 2, 3) | |
print "\n" | |
while-loop | |
def sum_with_while(num1, num2, num3) | |
numbers = [num1, num2, num3] | |
total_sum = 0 | |
i = 0 | |
while i < numbers.length do | |
total_sum += numbers[i] | |
i += 1 | |
# print total_sum | |
end | |
return total_sum | |
end | |
recursion | |
def sum_recursion(num1, num2, num3) | |
numbers = [num1, num2, num3] | |
@total_sum = 0 | |
index = 0 | |
def add_numbers(numbers, index) | |
if index >= numbers.length | |
return @total_sum | |
else | |
@total_sum += numbers[index] | |
index += 1 | |
add_numbers(numbers, index) | |
end | |
end | |
add_numbers(numbers, index) | |
end | |
PROBLEM 2 | |
Write a function that combines two lists by alternatingly taking elements | |
array_one = [1, 2, 3] | |
array_two = ['a', 'b', 'c'] | |
def combine_arrays(array1, array2) | |
array3 = [] | |
i = 0 | |
while i < array1.length do | |
array3 << array1[i] | |
array3 << array2[i] | |
i += 1 | |
end | |
return array3 | |
end | |
print combine_arrays(array_one, array_two) | |
PROBLEM 3 | |
Write a function that computes the list of the first 100 Fibonacci numbers | |
(by definition, the first two numbers in the Fibonacci sequence are 0 and 1, | |
each subsequent number is the sum of the previous two. | |
def fibonacci_sequence | |
fib_nums = [0, 1] | |
until fib_nums.length == 100 do | |
last_num = fib_nums.length - 1 | |
second_last_num = fib_nums.length - 2 | |
new_num = fib_nums[last_num] + fib_nums[second_last_num] | |
fib_nums << new_num | |
end | |
return fib_nums | |
end | |
print fibonacci_sequence | |
PROBLEM 4 | |
Write a function that given a list of non-negative integers, arranges | |
them such that they form the largest possible number | |
Exampe: give [50, 2, 1, 9] the largest formed number is 95021 | |
def generate_largest_number(array) | |
permutations = array.permutation.to_a | |
@largest_permutation | |
i = 0 | |
while i < permutations.length do | |
if i == 0 | |
@largest_permutation = permutations[i].join | |
else | |
if permutations[i].join > @largest_permutation | |
@largest_permutation = permutations[i].join | |
end | |
end | |
i += 1 | |
end | |
return @largest_permutation | |
end | |
puts generate_largest_number([40, 9, 23, 21, 2]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment