-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
44 additions
and
12 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# Required concepts: https://www.w3schools.com/python/python_intro.asp Everything till for loops | ||
|
||
print('Question: repeat every character twice') | ||
|
||
li = ['a' , 'b' , 'c' , 'd'] | ||
# we want ['aa' , 'bb' , 'cc' , 'dd'] | ||
|
||
for i in range(len(li)): | ||
li[i] = li[i]*2 | ||
# all elements repeat twice so *2 | ||
|
||
#single line let's repeat them again | ||
result = [letter*2 for letter in li] | ||
print('Answer:') | ||
print(result) | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# Required concepts: https://www.w3schools.com/python/python_intro.asp Everything till for loops | ||
|
||
print('Question: return reverse first half of l1 and reverse of second half of l2') | ||
|
||
l1 = ['a' , 'b' , 'c' , 'd'] | ||
l2 = ['e' , 'f' , 'g' , 'h'] | ||
|
||
# reverse first half of l1 and second of l2 | ||
# we want: ['b' , 'a' , 'h' , 'g'] | ||
|
||
# cut the first list into first half reverse and second into second half reverse | ||
half1 = len(l1)//2 - 1 | ||
half2 = len(l2)//2 - 1 | ||
# -1 for 0 based indicing | ||
result = l1[half1::-1] + l2[:half2:-1] | ||
|
||
print('Answer:') | ||
print(result) | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,16 @@ | ||
# Required concepts: https://www.w3schools.com/python/python_intro.asp Everything till for loops | ||
|
||
print('Question: count occurance of 2 in a number') | ||
print('Question: repeat every character twice') | ||
|
||
number = '222222222223456' | ||
count = 0 | ||
li = ['a' , 'b' , 'c' , 'd'] | ||
# we want ['aa' , 'bb' , 'cc' , 'dd'] | ||
|
||
# go through all the digits, check if it is 2 | ||
for digit in number: | ||
if digit == '2': | ||
count += 1 | ||
|
||
# single line | ||
count = len(number) - len(number.replace('2','')) | ||
for i in range(len(li)): | ||
li[i] = li[i]*2 | ||
# all elements repeat twice so *2 | ||
|
||
#single line let's repeat them again | ||
result = [letter*2 for letter in li] | ||
print('Answer:') | ||
print('2 has ',count,'occurances') | ||
print(result) | ||
|
||
#If you are same as your reverse, you are a palindrome |