-
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
4 changed files
with
98 additions
and
25 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,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 | ||
""" |
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,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)) |
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,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') | ||
|
||
|
||
|
||
|
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,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)) | ||
|
||
|
||
|
||
|
||
|