Skip to content

Commit fbdc33f

Browse files
committed
Day3 added
1 parent feb98f8 commit fbdc33f

File tree

4 files changed

+92
-0
lines changed

4 files changed

+92
-0
lines changed

Day3/binarysearch.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
def search(nums,target):
2+
nums.sort()
3+
beg,end=0,len(nums)-1
4+
while beg<=end:
5+
mid = (beg+end)//2
6+
if nums[mid]==target:
7+
return mid
8+
elif target>nums[mid]:
9+
beg=mid+1
10+
else:
11+
end=mid-1
12+
return -1
13+
14+
target = int(input("Enter a target element"))
15+
print(search([1,22,4,56,446,56,2,5],target))

Day3/bubblesort.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
def bubble_sort(container):
2+
for i in range(len(container)):
3+
for j in range(i+1,len(container)):
4+
if container[i]>container[j]:
5+
container[i],container[j]=container[j],container[i]
6+
7+
return container
8+
9+
print(bubble_sort([5,1,4,2,8]))

Day3/collatz.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
""""
2+
Write a program that lets the user type in an integer and that keeps calling collatz() on that number until the function returns the value 1. (Amazingly enough, this sequence actually works for any integer sooner or later, using this sequence, you'll arrive at 1! Even mathematicians aren't sure why. Your program is exploring what's called the Collatz sequence, sometimes called the simplest impossible math problem.) Remember to convert the return value from input() to an integer with the int() function; otherwise, it will be a string value. Hint: An integer number is even if number % 2 == 0, and it's odd if number % 2 == 1. The output of this program could look something like this:
3+
4+
Sample input: 3
5+
Sample output:
6+
10
7+
5
8+
16
9+
8
10+
4
11+
2
12+
1
13+
14+
Sample input: 5
15+
Sample output:
16+
16
17+
8
18+
4
19+
2
20+
1
21+
"""
22+
def collatz(number):
23+
try:
24+
num=int(input(number))
25+
while num>1:
26+
if num%2==0:
27+
num=k=num//2
28+
print(k)
29+
else:
30+
num=g=num*3+1
31+
print(g)
32+
except ValueError:
33+
print('Number should be integer and not string')
34+
35+
collatz('Enter a number:')

Day3/random_password_generator.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import random
2+
import string
3+
4+
def satisfied():
5+
while True:
6+
response=input('Are you Satisfied?(Y/n)').lower()
7+
if response[0] not in ['y','n']:
8+
print("***Invalid entry(enter y/n)***")
9+
continue
10+
if response.startswith('y'):
11+
return True
12+
else:
13+
return False
14+
15+
def main():
16+
upper_case = string.ascii_uppercase
17+
lower_case = string.ascii_lowercase
18+
numbers=string.digits
19+
symbols = string.punctuation
20+
password = []
21+
for _ in range(2):
22+
password.append(random.choice(upper_case)+random.choice(numbers)+random.choice(symbols))
23+
for _ in range(8):
24+
password.append(random.choice(lower_case))
25+
print(''.join(password))
26+
if satisfied():
27+
print("Your Password is:{}".format(''.join(password)))
28+
print("Thank You")
29+
else:
30+
main()
31+
32+
main()
33+

0 commit comments

Comments
 (0)