Skip to content

Commit 9bb32a0

Browse files
committed
all chapter added
1 parent 8962882 commit 9bb32a0

25 files changed

+1356
-6
lines changed

Harder/Generate-Sentences.md

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# 12.5: Generate Sentences [Premium]
2+
3+
## The Problem
4+
You have three lists of words. Create all possible combinations of sentences by taking one element from each list.
5+
6+
## Hint
7+
For each list, run a for loop for its len. This would be nested three for loops.
8+
9+
Under the last loop, access elements from each loop and show them as output.
10+
11+
## Solution
12+
```python
13+
subjects=["I", "You"]
14+
verbs=["Play", "Love"]
15+
objects=["Hockey","Football"]
16+
for i in range(len(subjects)):
17+
for j in range(len(verbs)):
18+
for k in range(len(objects)):
19+
print (subjects[i], verbs[j], objects[k])
20+
```
21+
**[Try it on Programming Hero](https://play.google.com/store/apps/details?id=com.learnprogramming.codecamp)**
22+
23+
## Explanation
24+
It's simple...Just run three for loops for three lists you have, each one under another. And finally, under the last one, access all three.
25+
26+
Damn easy, isn't it?
27+
28+

Harder/Password-generator.md

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# 12.2: Password generator
2+
3+
## The Problem
4+
5+
Generate a password. Your password may contain letters in uppercase or lowercase. It also may contain digits or special characters.
6+
7+
## Hints
8+
To select a random character from a string, you can import random. Then use the random. choice method. This will select a character from the provided string.
9+
10+
Also, you can import the string module.
11+
12+
This is not just the string type variable. Instead, it has a lot of extra functionalities.
13+
14+
For example, you can use string.ascii_letters to get all the characters a to z both in lowercase and upper case. Similarly, you can use string.digits to get all the single digits. Also, you can use string.punctuation to get all the special characters.
15+
16+
```python
17+
import string
18+
print('All letters')
19+
print(string.ascii_letters)
20+
print('all digits')
21+
print(string.digits)
22+
print('all special characters')
23+
print(string.punctuation)
24+
```
25+
26+
**[Try it on Programming Hero](https://play.google.com/store/apps/details?id=com.learnprogramming.codecamp)**
27+
28+
## Solution
29+
```python
30+
import string
31+
import random
32+
33+
def generate_password(size):
34+
all_chars = string.ascii_letters + string.digits + string.punctuation
35+
password = ''
36+
for char in range(size):
37+
rand_char = random.choice(all_chars)
38+
password = password + rand_char
39+
return password
40+
41+
pass_len = int(input('How many characters in your password?'))
42+
new_password = generate_password(pass_len)
43+
print('Your new password: ', new_password)
44+
```
45+
**[Try it on Programming Hero](https://play.google.com/store/apps/details?id=com.learnprogramming.codecamp)**
46+
47+
48+
## Explanation
49+
The solution is rather simple. We imported the random module and the string module. Then we created a long string by joining all ascii_letters, digits and special characters.
50+
51+
For that, we ran a for loop. In the loop, we select a random letter from the all_chars. To select a random character, we used random.choice. Then we add the random character to the password.

Harder/Password-with-requirements.md

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# 12.3: Password with Requirements [Premium]
2+
3+
## The Problem
4+
Generate a password that has a minimum of one uppercase, one lowercase, one digit, and one special character
5+
6+
## Hint
7+
This one is easier. The string module has two more things to get the uppercase and lower case characters...
8+
9+
```python
10+
import string
11+
print('All letters')
12+
print(string.ascii_letters)
13+
print('Al lowercase characters')
14+
print(string.ascii_lowercase)
15+
print('All uppercase characters')
16+
print(string.ascii_uppercase)
17+
```
18+
19+
**[Try it on Programming Hero](https://play.google.com/store/apps/details?id=com.learnprogramming.codecamp)**
20+
21+
## Solution
22+
```python
23+
import random
24+
import string
25+
26+
def randomPassword(size):
27+
all_chars = string.ascii_letters + string.digits + string.punctuation
28+
password = ""
29+
password += random.choice(string.ascii_lowercase)
30+
password += random.choice(string.ascii_uppercase)
31+
password += random.choice(string.digits)
32+
password += random.choice(string.punctuation)
33+
34+
for i in range(size-4):
35+
password += random.choice(all_chars)
36+
return password
37+
38+
pass_len = int(input("What would be the password length? "))
39+
print ("First Random Password is:", randomPassword(pass_len))
40+
print ("Second Random Password is:", randomPassword(pass_len))
41+
print ("Third Random Password is:", randomPassword(pass_len))
42+
```
43+
**[Try it on Programming Hero](https://play.google.com/store/apps/details?id=com.learnprogramming.codecamp)**
44+
45+
## Explanation
46+
The all_chars variable is the same as before. After that, we created a password variable with an empty string.
47+
48+
Then, we added random choice from lowercase. This way, we will make sure that at least one lowercase character is added in the password. Similarly, we added one uppercase, one digit, and one special character.
49+
50+
After that, we ran a for loop to select random characters from the all_chars. This is similar to the previous problem. We just ran the for loop for (size-4) times, because we already added 4 characters in the password before the loop.
51+
52+
Make sense?
53+
54+
Keep going. Only a few more left. I am sure you can do it
55+

Harder/Permutations.md

+92
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
# 12.4: Permutation [Premium]
2+
3+
## The Problem
4+
Find all permutation of a given list.
5+
6+
## Hints
7+
In mathematics, the permutation is the way to find all possible order of elements
8+
9+
For example, you have 1,2,3. Now all possible order is
10+
1,2,3
11+
1,3,2
12+
2,1,3
13+
2,3,1
14+
3,1,2
15+
3,2,1
16+
17+
So, the permutation is a way to find all the possible order or sequence of elements.
18+
19+
Give me more: non-premium
20+
One important part of the solution is to select one element and get a list excluding that element. Let's say you want to select the list without the 4th position element.
21+
22+
All the elements before the 4th index element will be lst[:4]. This is from beginning to before the 4th element.
23+
24+
Similarly, all the elements after the fourth position will be lst[4+1:]. This is from the 4+1 or 5th element to the end.
25+
26+
This concept will help in permutation to select one element and place it in multiple places.
27+
28+
```python
29+
lst = [2,3,4,6,11,16,7,8]
30+
before = lst[:4]
31+
after = lst[4+1:]
32+
excluding = before + after
33+
print(excluding)
34+
print('-----------------------')
35+
print(lst[:4] + lst[4+1])
36+
```
37+
38+
**[Try it on Programming Hero](https://play.google.com/store/apps/details?id=com.learnprogramming.codecamp)**
39+
40+
41+
## Solution
42+
43+
```python
44+
def get_permutation(lst):
45+
# For an empty list, there is no permutation
46+
if len(lst) == 0:
47+
return []
48+
# list with one element will have only one
49+
# permutation
50+
if len(lst) == 1:
51+
return [lst]
52+
# Create an empty list to store permutation
53+
perms = []
54+
for i in range(len(lst)):
55+
# Extract current elemnt from the list.
56+
current = lst[i]
57+
# Recursively call permutation for the
58+
# remaining list
59+
rem_list = lst[:i] + lst[i+1:]
60+
rem_perm = get_permutation(rem_list)
61+
# Generate permutations by adding first element
62+
for p in rem_perm:
63+
perms.append([current] + p)
64+
return perms
65+
# now test the function
66+
data = [1,2,3]
67+
for perm in get_permutation(data):
68+
print (perm)
69+
```
70+
71+
**[Try it on Programming Hero](https://play.google.com/store/apps/details?id=com.learnprogramming.codecamp)**
72+
73+
74+
## Explanation
75+
In the code, we are getting the current element by lst[i].
76+
Then we create the remaining list by lst[:i] + lst[i+1:]
77+
78+
We stored it in the remaining list named as rem_list. After that, we recursively call the get_permutation. This will give permutation for the rest elements. We stored that in the rem_perm list.
79+
80+
Finally, we have to join the current element with the rest of the list. To do so, we have added [current] + p
81+
82+
The loop variable p is a list. Whereas the current is an element. You can not add
83+
84+
3 + [2,1]
85+
86+
That’s why we put current in a list as well so that it becomes a list as well. In the following example,
87+
88+
[3] + [2, 1]
89+
90+
This will create [3, 2, 1]. That’s why we did [current] + p to make the final permutation.
91+
92+
This code is harder. It will take a few practices to become comfortable with it.

Harder/Simple-Calculator.md

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# 12.1 Simple Calculator
2+
3+
## The task
4+
Create a simple calculator. That will be able to take user input of two numbers and the operation the user wants to perform.
5+
6+
## Solution strategy
7+
Create a bunch of functions to perform add, subtract, multiply, division or modulo.
8+
9+
Then take two numbers from the user and the operation he/she wants to perform. Either +,-,*,/ or %.
10+
11+
Then call the appropriate function based on the operation.
12+
13+
Think for a few minutes and try it yourself first.
14+
15+
## The solution
16+
```python
17+
def add(num1, num2):
18+
return num1 + num2
19+
20+
def subtract(num1, num2):
21+
return num1 - num2
22+
23+
def multiply(num1, num2):
24+
return num1 * num2
25+
26+
def divide(num1, num2):
27+
return num1 / num2
28+
29+
def modulo(num1, num2):
30+
return num1 % num2
31+
32+
# Take input from the user
33+
num1 = int(input("Enter first number: "))
34+
operation = input("What you want to do(+, -, *, /, %):")
35+
num2 = int(input("Enter second number: "))
36+
37+
result = 0
38+
if operation == '+':
39+
result = add(num1,num2)
40+
elif operation == '-':
41+
result = subtract(num1,num2)
42+
elif operation == '*':
43+
result = multiply(num1,num2)
44+
elif operation == '/':
45+
result = divide(num1,num2)
46+
elif operation == '%':
47+
result = modulo(num1,num2)
48+
else:
49+
print("Please enter: +, -, *, / or %")
50+
51+
print(num1, operation, num2, '=', result)
52+
```
53+
**[Try it on Programming Hero](https://play.google.com/store/apps/details?id=com.learnprogramming.codecamp)**
54+
55+
56+
## How it works
57+
You saw five functions to add, subtracts, etc. Those are easy.
58+
59+
Then we are taking user inputs. Three inputs. They are easy too.
60+
61+
Then we have if-elif-else. And based on the operation, we call the right method to perform the task.
62+
63+
That’s it.
64+
65+

Medium/Armstrong-number.md

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
# 9.3: Armstrong number.
2+
3+
## The problem
4+
Check whether a number is an Armstrong number.
5+
6+
## Armstrong number
7+
Armstrong is a special number.
8+
9+
A number is an Armstrong Number or narcissistic number if it is equal to the sum of its own digits raised to the power of the number of digits.
10+
11+
Think about the number 371. The total number of digits is 3. Now, for each digit, put the power of 3 and then add them. It will be:
12+
13+
```
14+
33 + 73 + 13
15+
= 27 + 343 + 1
16+
= 371
17+
Hence, 371 is an Armstrong number.
18+
```
19+
20+
Similarly, 1634 is another Armstrong number, because the total number of digits is 4. Now, power each digit and sum them. You will get the Armstrong number.
21+
22+
```
23+
= 14 + 64 + 34 + 44
24+
= 1 + 1296 + 81 + 256
25+
= 1634
26+
```
27+
28+
29+
## The solution
30+
31+
```python
32+
def check_armstrong(num):
33+
order = len(str(num))
34+
35+
sum = 0
36+
37+
# use temp to find each digit. Then power it
38+
temp = num
39+
while temp > 0:
40+
digit = temp % 10
41+
sum += digit ** order
42+
temp //= 10
43+
return num == sum
44+
45+
num = int(input('Enter your number: '))
46+
47+
if check_armstrong(num):
48+
print(num,"is an Armstrong number")
49+
else:
50+
print(num,"is not an Armstrong number")
51+
```
52+
**[Try it on Programming Hero](https://play.google.com/store/apps/details?id=com.learnprogramming.codecamp)**
53+
54+
## Explanation
55+
Three things could be new for you.
56+
57+
First of all, to know the total number of digits in a number, we converted the number to a string. We used the str(num) to convert it to the string. And then we pass it to the len function.
58+
59+
This mean, len function will tell you how many characters are on that string.
60+
61+
Another thing we did is to store the input number in a temp variable. We had to do that because, while getting the digits, we are dividing the number by 10.
62+
63+
However, at the end, we need to compare the result of the while loop in the sum with the original number.
64+
65+
That's why, we set the num to the temp variable. And then, we divided the temp variable. In this way, the num variable remained the same.
66+
67+
Finally, return num == sum means if num is equal to the sum, it will return True. Otherwise, it will return False.
68+
69+
Hence, return num == sum is a shortcut method of:
70+
71+
```python
72+
result = False
73+
if num == sum:
74+
result = True
75+
else:
76+
result = False
77+
return result
78+
```
79+
80+

0 commit comments

Comments
 (0)