Skip to content

Instantly share code, notes, and snippets.

@amrdraz
Created November 2, 2015 11:32
Show Gist options
  • Save amrdraz/67cd9d1d20c8f170d7eb to your computer and use it in GitHub Desktop.
Save amrdraz/67cd9d1d20c8f170d7eb to your computer and use it in GitHub Desktop.
loop problems
"""
### Problem
Given an even number n, write an algorithem that returns how many times we can divide it by 2 before it reaches an odd number
### Example
<pre class="no-run">
input:
64
output:
6
</pre>
<pre class="no-run">
input:
34
output:
6
</pre>
<pre class="no-run">
input:
2
output:
1
</pre>
"""
# Solution
"""================================================================="""
"""
### Problem
Given a number n, write an algorithem that counts the number of 3s in that number
### Example
<pre class="no-run">
input:
1234
output:
1
</pre>
<pre class="no-run">
input:
343
output:
2
</pre>
<pre class="no-run">
input:
35733
output:
3
</pre>
"""
# Solution
"""================================================================="""
"""""
:modulus :loops
### Problem: CozaLozaWoza
Write a program called CozaLozaWoza which prints the numbers 1 to n.
The program shall print
- `"Coza"` in place of the numbers which are multiples of 3
- `"Loza"` for multiples of 5
- `"Woza"` for multiples of 7
- `"CozaLoza"` for multiples of 3 and 5
- `"CozaWoza"` for multiples of 3 and 7
- `"WozaLoza"` for multiples of 5 and 7
### Example
<pre class="no-run">
input:
n: 35
output:
1 2 Coza 4 Loza Coza Woza 8 Coza Loza 11 Coza 13 Woza CozaLoza 16 17 Coza 19 Loza CozaWoza 22 23 Coza Loza 26 Coza Woza 29 CozaLoza 31 32 Coza 34 WozaLoza
</pre>
"""
# Solution
"""================================================================="""
"""================================================================="""
"""""
:modulus :loops
### Problem: CozaLozaWoza
Write a program that given a number, checks if the digits
In the even positions are even and displays to the user True, or displayes False if not.
The rightmost digit is at position 1.
### Example
<pre class="no-run">
input:
561
output:
True
</pre>
<pre class="no-run">
input:
12986
output:
True
</pre>
<pre class="no-run">
input:
112
output:
False
</pre>
"""
# Solution
"""================================================================="""
"""================================================================="""
"""""
:while :loop :20
### Problem: CozaLozaWoza
Write a java program that given two integer inputs 'n' and 'r',
will output ‘n’ a total of ‘r’ times followed by a separator.
The separator is calculated by taking the integer division of the last digit of the number by 2.
### Example
<pre class="no-run">
input:
n: 1576
r: 4
output:
1576315763157631576
</pre>
<pre class="no-run">
input:
n: 2359
r: 3
output:
23594235942359
</pre>
"""
# Solution
"""================================================================="""
"""================================================================="""
"""""
:while :loop :20
### Problem: CozaLozaWoza
Write an algorithm that counts how many times the last digit in a number is repeated.
### Example
<pre class="no-run">
input:
32353253
output:
4
</pre>
<pre class="no-run">
input:
61212255
output:
2
</pre>
"""
# Solution
"""================================================================="""
"""================================================================="""
"""================================================================="""
"""""
:while :loop :20 :list :strings
### Problem: replace with k
Write an algorithm that replaces every other number with k
### Example
<pre class="no-run">
input:
2004554323
7
output:
7074757373
</pre>
<pre class="no-run">
input:
602568283
1
output:
101518181
</pre>
"""
# Solution
"""================================================================="""
"""================================================================="""
"""""
:while :loop :20 :conditional
### Problem: conditional series
Write a program that computes the following Series:
In case n is even: (1*2) + (3*4) + ......... + ((n­-1)*n)
In case n is odd: (1*2) + (3*4) + ......... + n
### Example
<pre class="no-run">
input:
6
output:
44
</pre>
<pre class="no-run">
input:
5
output:
19
</pre>
"""
# Solution
"""================================================================="""
"""================================================================="""
"""""
:while :loop :20 :modulus :import :math
### Problem: Armstrong Number
An Armstrong number is a 3-digit number for which the sum of cube of its digit is equal to the number.
For example an Armstrong number is `153` as `153 = 1 + 125 + 27` which is equal to `1**3+5**3+3**3`.
Another Armstrong number is `371 = 3**3+7**3+1**3`.
### Example
<pre class="no-run">
input:
153
output:
True
</pre>
<pre class="no-run">
input:
5
output:
False
</pre>
"""
# Solution
"""================================================================="""
"""""
:while :loop :20 :strings
### Problem: Mirror
Write a program that given 2 strings __a__ and __b__ checks wether they are mirror of each other and prints a boolean value
### Example
<pre class="no-run">
input:
57692
29675
output:
True
</pre>
<pre class="no-run">
input:
hello
olleh
output:
True
</pre>
"""
"""================================================================="""
"""""
:while :loop :20
### Problem: Mirror
Write a program that takes as an input an integer number __n__ and divides it by 2 if it is an even number.
If it is an odd number it should multiply it by 3 and add 1. This should be repeated until the value becomes 1 and every time the value should be printed.
For example your program should run as follows:
### Example
<pre class="no-run">
input:
13
output:
40
20
10
5
16
8
4
2
1
</pre>
"""
"""================================================================="""
"""""
:while :loop :20 :modulus
### Problem: Mirror
Write a program that given as input three numbers X, Y and Z.
prints out the numbers divisible by Z between X and Y.
Assume that X is smaller than Y.
### Example
<pre class="no-run">
input:
x: 2
y: 10
z: 3
output:
3
6
9
</pre>
"""
"""================================================================="""
"""""
:while :loop :20 :modulus :strings
we shoudl have the test tag wether they used a modulus or a string solution
----
### Problem: Mirror
Write a Java program that given a number n, computes the sum of the multiplication of every 2 consecutive digits in n
> If n has an odd number of digits, the leftmost digit is just added to the result
### Example
<pre class="no-run">
input:
123456
output:
44
</pre>
Explanation 1:
44 = (1 * 2) + (3 * 4) + (5 * 6)
<pre class="no-run">
input:
23653
output:
35
</pre>
Explanation 2:
35 = (2) + (3 * 6) + (5 * 3)
"""
"""================================================================="""
"""""
:while :loop :20 :modulus :strings
we shoudl have the test tag wether they used a modulus or a string solution
----
### Problem: Mirror
Write a program that prints the smallest and the largest digit in a number.
If all the digits of the number are equal the algorithm should print "This is redundant"
### Example
<pre class="no-run">
input:
1234
output:
largest 4
smallest 1
</pre>
<pre class="no-run">
input:
5690
output:
largest 9
smallest 0
</pre>
<pre class="no-run">
input:
555
output:
This is redundant
</pre>
"""
"""================================================================="""
"""""
:while :loop :20
----
### Problem: Bunnies In A Line
We have n bunnies standing in a line
The sequence of number of bunny ears is as follows:
2 ears, 3 ears, 4 ears and the sequence repeats.
If you’re wondering how 3 and 4 ears; because they each have either one or both raised foot.
> You are not allowed to use multiplication
### Example
<pre class="no-run">
input:
4
output:
11
</pre>
<pre class="no-run">
input:
÷≥5
output:
14
</pre>
<pre class="no-run">
input:
6
output:
16
</pre>
"""
"""================================================================="""
"""""
:while :loop :20 :modulus :strings
----
### Problem: Bunnies In A Line
Write a program that generates the following.
Given a number sequence, counts the number of 1s, 2s, 3s, 4s, and 5s in the number.
then add to the original number as follows
- for every two 1s add 100
- for every 2 add 20
- for every 3 add 30
- for every 4 add 40
- for every 5 add 50.
- you do nothing for all other numbers.
### Example
<pre class="no-run">
input:
152231
output:
152431
</pre>
<pre class="no-run">
input:
1520
output:
1590
</pre>
<pre class="no-run">
input:
1213111
output:
1213361
</pre>
"""
"""================================================================="""
"""""
:while :loop :20 :modulus :strings
----
### Problem: Step Numbers
A number is said to be a step number if for each pair of consecutive digits in the number,
their difference is 1 (Ex. 456567).
Write a program that given a number, prints whether it is a step number or not.
Single digit numbers can be considered as step numbers.
### Example
<pre class="no-run">
input:
456567
output:
True
</pre>
<pre class="no-run">
input:
4920
output:
True
</pre>
""""""================================================================="""
"""""
:while :loop :20 :modulus :strings
----
### Problem: Step Numbers
Write a program that given a number consisting of more than 2 digits, you should calculate the average of its digits.
### Example
<pre class="no-run">
input:
34788
output:
6
</pre>
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment