-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path9-2-21.js
More file actions
123 lines (73 loc) · 3.2 KB
/
Copy path9-2-21.js
File metadata and controls
123 lines (73 loc) · 3.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// Recursion #2 - Fibonacci
// Do you know recursion?
// This is a kata series that you can only solve using recursion.
// ##2 - Fibonacci number
// In mathematical terms, the sequence f(n) of fibonacci numbers is defined by the recurrence relation
// f(n) = f(n-1) + f(n-2)
// with seed values
// f(1) = 1 and f(2) = 1
// #Your task
// You have to create the function fibonacci that receives n and returns f(n). You have to use recursion.
const fibonacci = n => {
if (n==0 || n==1) return n
else return fibonacci(n-1) + fibonacci(n-2)
};
//Recursive Ninja
// Ninjas frequently need to signal each other in code, often employing natural sounds as a cover. We’re going to give our ninja the ability to chirp like a cricket, with the number of chirps encoding different messages.
// Implement a "recursive function" that takes one parameter (n) and outputs a string i.e.
// chirp(4);
// //output would be chirp-chirp-chirp-chirp.
function chirp(n) {
if (n==0) return 'chirp'
else if(n==1) return 'chirp'
else return chirp(n-1) + '-' + chirp(0)
}
//Fibonacci Reloaded
// And here is Fibonacci again. This time we want to go one step further. Our fib() function must be faster! Can you do it?
// In case you don't know, what the Fibonacci number is:
// The nth Fibonacci number is defined by the sum of the two previous Fibonacci numbers. In our case: fib(1) := 0 and fib(2) := 1. With these initial values you should be able to calculate each following Fibonacci number.
// Examples:
// fib(1) // === 0
// fib(2) // === 1
// fib(3) // === 1
// fib(4) // === 2
// fib(5) // === 3
function fib(num) {
var a = 0, b = 1, temp;
while (num >= 1){
temp = a;
a = a + b;
b = temp;
num--;
}
return b;
}
//Heads and Legs
// #Description
// Everybody has probably heard of the animal heads and legs problem from the earlier years at school. It goes:
// “A farm contains chickens and cows. There are x heads and y legs. How many chickens and cows are there?”
// Where x <= 1000 and y <=1000
// #Task
// Assuming there are no other types of animals, work out how many of each animal are there.
// Return a tuple in Python - (chickens, cows) and an array list - [chickens, cows]/{chickens, cows} in all other languages
// If either the heads & legs is negative, the result of your calculation is negative or the calculation is a float return "No solutions" (no valid cases).
// In the form:
// [Heads, Legs] = [72, 200]
// VALID - [72, 200] => [44 , 28]
// [Chickens, Cows]
// INVALID - [72, 201] => "No solutions"
// However, if 0 heads and 0 legs are given always return [0, 0] since zero heads must give zero animals.
// There are many different ways to solve this, but they all give the same answer.
// You will only be given integers types - however negative values (edge cases) will be given.
// Happy coding!
function animals(heads, legs){
//return [Chickens, Cows]
let chickens = heads
let cows = 0
for (let i = chickens ; i >= 0 ; i--) {
let guessLegs = i*2 + cows*4
if (guessLegs === legs) return [i,cows]
cows++
}
return 'No solutions'
}