Skip to content

Commit

Permalink
repeat the chars
Browse files Browse the repository at this point in the history
  • Loading branch information
yozaam committed Oct 20, 2020
1 parent 0ca4707 commit 39f13ad
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 12 deletions.
16 changes: 16 additions & 0 deletions day2_repeat_chars.py
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)

19 changes: 19 additions & 0 deletions day2_reverse_halves.py
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)

21 changes: 9 additions & 12 deletions main.py
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

0 comments on commit 39f13ad

Please sign in to comment.