Skip to content

Commit

Permalink
day 4 problems
Browse files Browse the repository at this point in the history
  • Loading branch information
yozaam committed Oct 24, 2020
1 parent 7351bbb commit 9d741e9
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 25 deletions.
41 changes: 41 additions & 0 deletions day4_josephus_brute_force.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Required concepts: https://www.w3schools.com/python/python_intro.asp strings

print('Question: Shoot your neighbours, Josephus Problem')

n = int(input('how many ppl stand in circle: '))

kills = 0
cur_gun_holder = 0
to_shoot = -1
dead = set()

while cur_gun_holder != to_shoot:
# who to shoot?
# whoever is alive and next to me!!!!
to_shoot = (cur_gun_holder+1)%n
while to_shoot in dead:
to_shoot = (to_shoot+1)%n
print(cur_gun_holder+1,'shoots',to_shoot+1)
if cur_gun_holder == to_shoot:
break
# now we kill him
dead.add(to_shoot)
kills += 1

# who gets the gun ?
# next alive guy
cur_gun_holder = (cur_gun_holder+1)%n
while cur_gun_holder in dead:
cur_gun_holder = (cur_gun_holder+1)%n

print('Answer:')
print(cur_gun_holder, 'is alive')

"""
# Superb Circular Linked List Solution!
while node.next:
node.next = node.next.next # kill your next!
node = node.next # pass the gun
"""
15 changes: 15 additions & 0 deletions day4_max_product.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Required concepts: https://www.w3schools.com/python/python_intro.asp lists

print('Question: Maximum product of two elements in array')



li = [-7,-6,4,1,2,3,4,5,6]
li.sort()
# 5*6 or -7*-6 only those could give me the maximum
# product of the two smallest, neg*neg
first = li[0]*li[1]
last = li[-1]*li[-2]

print('Answer:')
print(max(first,last))
29 changes: 29 additions & 0 deletions day4_print_rotations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Required concepts: https://www.w3schools.com/python/python_intro.asp strings

print('Question: Print all rotations of a list')

# suboptimal is :
#
#for i in range(len(li)):
# print(li[-i:] + lst[:-i])


# this is the optimal solution, without any extra space, since we are just printing!

li = [1,2,3,4,5,6]
n = len(li)
# for each rotation you start at the new start
for new_start in range(n):
next_ = (new_start + 1)%n
print('\n',li[new_start], end=' ')
while next_ != new_start:
print(li[next_],end=' ')
next_ = (next_+1)%n
# keep printing in circular fashion until you reach the start poiint :D


print('\nare all rotations, our answer')




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

print('Question: reverse each word of string')
print('Question: Maximum product of two elements in array')

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

for word in sentence.split():
# get each word from the split function
res += word[::-1]+" "
#add the reverse of it

li = [-7,-6,4,1,2,3,4,5,6]
li.sort()
# 5*6 or -7*-6 only those could give me the maximum
# product of the two smallest, neg*neg
first = li[0]*li[1]
last = li[-1]*li[-2]

print('Answer:')
print(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(max(first,last))





0 comments on commit 9d741e9

Please sign in to comment.