Skip to content

Commit

Permalink
day 3 pairs of two lists
Browse files Browse the repository at this point in the history
  • Loading branch information
yozaam committed Oct 23, 2020
1 parent 537bcbd commit 9922c48
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 13 deletions.
18 changes: 18 additions & 0 deletions day3_pairs_of_two_lists.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Required concepts: https://www.w3schools.com/python/python_intro.asp strings

print('Question: for sets A and B return all pairs {A(i) , B(-i)}')


l1 = ["hi","hello","bye"]
l2 = ["shivam",'raj','roy']

for i in range(len(l1)):
print(l1[i]+" "+l2[-(i+1)])
# match first element of l1,
# with last element of l2

#single line
res = [(l1[i],l2[-i-1]) for i in range(len(l1))]

print('Answer:')
print(res)
24 changes: 24 additions & 0 deletions day3_reverse_each_word.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Required concepts: https://www.w3schools.com/python/python_intro.asp Everything till for loops

print('Question: reverse each word of string')

sentence = 'hello world how are you'
res = ''

# what to reverse?
# reverse upto the space
left = 0
while left < len(sentence):
right = sentence.find(' ',left)
if right == -1:
right = len(sentence)
word = sentence[left:right]
# now add this reversed word to result!
print(res)
res += word[::-1]
left = right
#now start from here

print('Answer:')
print(res)

26 changes: 13 additions & 13 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
# Required concepts: https://www.w3schools.com/python/python_intro.asp lists
# Required concepts: https://www.w3schools.com/python/python_intro.asp strings

print('Question: simulate game where raj can pick the chocolate from either edge of list, he will always pick the bigger one')
print('Question: for sets A and B return all pairs {A(i) , B(-i)}')

chocolates = [1,3,4,5,6]

while len(chocolates) > 1:
#check who is bigger first or last?
if chocolates[0] > chocolates[-1]:
# what if first is bigger?
eat = chocolates.pop(0)
else:
eat = chocolates.pop()
print('Eating ',eat)
l1 = ["hi","hello","bye"]
l2 = ["shivam",'raj','roy']

print('Answer:')
print('Left with chocolate',chocolates[0])
for i in range(len(l1)):
print(l1[i]+" "+l2[-(i+1)])
# match first element of l1,
# with last element of l2

#single line
res = [(l1[i],l2[-i-1]) for i in range(len(l1))]

print('Answer:')
print(res)

0 comments on commit 9922c48

Please sign in to comment.