Skip to content

Commit d536abb

Browse files
committed
Add solutions
1 parent 412cdbb commit d536abb

File tree

5 files changed

+153
-4
lines changed

5 files changed

+153
-4
lines changed

README.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
# codingchallenges
2-
Answers to challenges from [r/DailyProgrammer](https://www.reddit.com/r/dailyprogrammer/), [Project Euler](https://projecteuler.net/archives), and [Daily Coding Problem](https://www.dailycodingproblem.com/) .
2+
Answers to various coding challenges and problems around the internet.
33

44
## Index
55

66
| Challenge | Problems List |
77
| --------- | ------------- |
8-
| Daily Programmer | [Index](../master/dailyprogrammer/index.md) |
9-
| Project Euler | [Index](../master/projecteuler/index.md) |
10-
| Daily Coding Problem | [Index](../master/dailycodingproblem/index.md) |
8+
| [r/DailyProgrammer](https://www.reddit.com/r/dailyprogrammer/) | [Index](../master/dailyprogrammer/index.md) |
9+
| [Project Euler](https://projecteuler.net/archives) | [Index](../master/projecteuler/) |
10+
| [Daily Coding Problem](https://www.dailycodingproblem.com/) | [Index](../master/dailycodingproblem/) |
11+
| [UVa Online Judge](https://uva.onlinejudge.org/) | [Index](../master/uvaonlinejudge/) |

dailycodingproblem/day_5.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Explanation: https://stackoverflow.com/questions/21769348/use-of-lambda-for-cons-car-cdr-definition-in-sicp
2+
3+
def cons(a, b):
4+
return lambda f: f(a, b)
5+
6+
7+
def car(cons):
8+
return cons(lambda a, b: a)
9+
10+
11+
def cdr(cons):
12+
return cons(lambda a, b: b)
13+
14+
15+
def main():
16+
print('car(cons(3, 4)) == 3:', car(cons(3, 4)) == 3)
17+
print('cdr(cons(3, 4)) == 4:', cdr(cons(3, 4)) == 4)
18+
19+
# [STEP BY STEP SOLUTION]
20+
21+
# cons(3, 4) == lambda f: f(3, 4)
22+
23+
# (lambda f: f(3, 4))(f) == 3
24+
# f(3, 4) == 3
25+
# f == (lambda a, b: a)
26+
27+
# (lambda a, b: a)(3, 4) == 3
28+
# f(3, 4) == 3
29+
30+
# (lambda f: f(3, 4))(lambda a, b: a) == 3
31+
# (cons(3, 4))(lambda a, b: a) == 3
32+
# car(cons(3, 4)) == 3
33+
34+
# car(cons(3, 4)) == (cons(3, 4))(lambda a, b: a)
35+
36+
37+
if __name__ == '__main__':
38+
main()

dailyprogrammer/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
| ID | Difficulty | Title | Solution |
44
| --- | ---------- | ----- | -------- |
5+
| 355 | Easy | Alphabet Cipher | :small_blue_diamond: [Python](../master/dailyprogrammer/python/355_easy.py)
56
| 354 | Intermediate | Integer Complexity 2 | :small_orange_diamond: [Python](../master/dailyprogrammer/python/354_intermediate.py)
67
| 354 | Easy | Integer Complexity 1 | :small_blue_diamond: [Python](../master/dailyprogrammer/python/354_easy.py)
78
| 353 | Intermediate | Pancake Sorting | :small_orange_diamond: [Python](../master/dailyprogrammer/python/353_intermediate.py)

dailyprogrammer/python/355_easy.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# https://www.reddit.com/r/dailyprogrammer/comments/879u8b/20180326_challenge_355_easy_alphabet_cipher/
2+
3+
from itertools import cycle
4+
5+
6+
def cipher(char, key, decipher=False):
7+
alpha = 'abcdefghijklmnopqrstuvwxyz'
8+
shift = ord(key.lower()) - 97
9+
if decipher:
10+
return alpha[(alpha[shift:] + alpha[:shift]).index(char.lower())]
11+
else:
12+
alpha = alpha[shift:] + alpha[:shift]
13+
return alpha[ord(char.lower()) - 97]
14+
15+
16+
def encrypt(text, key):
17+
return ''.join(cipher(v, k) for v, k in zip(text, cycle(key)))
18+
19+
20+
def decrypt(text, key):
21+
return ''.join(cipher(v, k, decipher=True) for v, k in zip(text, cycle(key)))
22+
23+
24+
def main():
25+
inputs = [
26+
'snitch thepackagehasbeendelivered',
27+
'bond theredfoxtrotsquietlyatmidnight',
28+
'train murderontheorientexpress',
29+
'garden themolessnuckintothegardenlastnight',
30+
]
31+
32+
for inp in inputs:
33+
key, text = inp.split()
34+
print('{} {} => {}'.format(key, text, encrypt(text, key)))
35+
36+
37+
def bonus():
38+
inputs = [
39+
'cloak klatrgafedvtssdwywcyty',
40+
'python pjphmfamhrcaifxifvvfmzwqtmyswst',
41+
'moore rcfpsgfspiecbcc',
42+
]
43+
44+
for inp in inputs:
45+
key, text = inp.split()
46+
print('{} {} => {}'.format(key, text, decrypt(text, key)))
47+
48+
49+
if __name__ == '__main__':
50+
main()
51+
bonus()

uvaonlinejudge/c/v1-100.c

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
4+
5+
int collatz(int num) {
6+
int counter = 1;
7+
8+
while (num > 1) {
9+
num = (num % 2 == 0) ? (num / 2) : (3 * num + 1);
10+
counter++;
11+
}
12+
13+
return counter;
14+
}
15+
16+
17+
int maxcollatz(int i, int j) {
18+
int n;
19+
int counter;
20+
int max = 0;
21+
22+
for (n = i; n <= j; n++) {
23+
counter = collatz(n);
24+
if (counter > max) {
25+
max = counter;
26+
}
27+
}
28+
29+
return max;
30+
}
31+
32+
33+
int main(void) {
34+
const char NEWLINE = '\n';
35+
int size;
36+
int i, j;
37+
38+
char line[512];
39+
40+
fgets(line, sizeof(line), stdin);
41+
sscanf(line, "%d", &size);
42+
43+
int input[size][2];
44+
for (int s = 0; s < size; ++s) {
45+
fgets(line, sizeof(line), stdin);
46+
sscanf(line, "%d %d", &input[s][0], &input[s][1]);
47+
}
48+
49+
for (int s = 0; s < size; ++s) {
50+
i = input[s][0];
51+
j = input[s][1];
52+
int max = maxcollatz(i, j);
53+
printf("%d %d %d", i, j, max);
54+
printf("%c", NEWLINE);
55+
}
56+
57+
return EXIT_SUCCESS;
58+
}

0 commit comments

Comments
 (0)