-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathloops.py
More file actions
266 lines (203 loc) · 7.49 KB
/
Copy pathloops.py
File metadata and controls
266 lines (203 loc) · 7.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
# ── LOOPS ─────────────────────────────────
# Loops repeat code multiple times
# Without loops → you'd write same code 100x
# With loops → write once, run 100x! 🔥
# ── 1. FOR LOOP ───────────────────────────
# Used when you know HOW MANY times to repeat
# Used in: lists, data processing, games
# basic for loop
for i in range(5):
print(i) # prints 0,1,2,3,4
# range with start and end
for i in range(1, 6):
print(i) # prints 1,2,3,4,5
# range with steps
for i in range(0, 10, 2):
print(i) # prints 0,2,4,6,8
#let's put some DSA problem using for loop
# ── 1. FIND MAXIMUM NUMBER IN LIST ────────
# Problem: find biggest number without max()
# Used in: sorting algorithms, data analysis
numbers = [34, 67, 23, 89, 12, 45]
maximum = numbers[0] # assume first is biggest
for num in numbers:
if num > maximum:
maximum = num # update if bigger found
print(f"Maximum: {maximum}") # 89
# ── 2. COUNT OCCURRENCES ──────────────────
# Problem: how many times does 3 appear?
# Used in: search engines, data processing
numbers = [1, 3, 5, 3, 7, 3, 9, 3]
count = 0
target = 3
for num in numbers:
if num == target:
count += 1
print(f"{target} appears {count} times") # 4 times
#this above two problem used in weather app ~ what is the highest temp of day etc.
'''
Find Maximum → any time you need
"who/what is the biggest/best/highest"
Count Occurrences → any time you need
"how many times does X appear"
'''
# ── 3. REVERSE A STRING ───────────────────
# Problem: reverse "afroj" → "jorfa"
# Asked in almost EVERY coding interview!
name = "Joey"
reversed_name = ""
for char in name:
reversed_name = char + reversed_name
print(f"Reversed: {reversed_name}") # jorfa
name = "conrad"
reversed_name = ""
for char in name:
reversed_name = char + reversed_name
print(f"Reversed: {reversed_name}")
'''
reverse a string ~ real world ex.
→ Encryption → reversing text to hide data
→ DNA research → reversing gene sequences
→ Undo feature → reverse last action
→ Palindrome check → needs reverse first!
→ URL shorteners → encode/decode links
'''
# ── 4. CHECK PALINDROME ───────────────────
# Problem: is "madam" same forwards/backwards?
# palindrome = reads same both ways
# Used in: string processing interviews
word = "madam"
reversed_word = ""
for char in word:
reversed_word = char + reversed_word
if word == reversed_word:
print(f"{word} is a palindrome! ")
else:
print(f"{word} is not a palindrome! ")
# ── 5. SUM OF DIGITS ──────────────────────
# Problem: sum digits of 1234 → 1+2+3+4 = 10
# Asked in beginner coding interviews!
number = "1234"
total = 0
for digit in number:
total += int(digit)
print(f"Sum of digits: {total}") # 10
# ── 6. FIND DUPLICATES ────────────────────
# Problem: find numbers that appear twice
# Used in: data cleaning, databases
numbers = [1, 2, 3, 2, 4, 3, 5]
seen = []
duplicates = []
for num in numbers:
if num in seen:
if num not in duplicates:
duplicates.append(num)
else:
seen.append(num)
print(f"Duplicates: {duplicates}") # [2, 3]
# ── 7. BUBBLE SORT ────────────────────────
# Problem: sort list from smallest to biggest
# Classic DSA sorting algorithm!
# Used in: databases, search results
numbers = [64, 34, 25, 12, 22, 11, 90]
for i in range(len(numbers)):
for j in range(0, len(numbers)-i-1):
if numbers[j] > numbers[j+1]:
# swap them!
numbers[j], numbers[j+1] = numbers[j+1], numbers[j]
print(f"Sorted: {numbers}") # [11, 12, 22, 25, 34, 64, 90]
# ── 8. FIBONACCI SEQUENCE ─────────────────
# Problem: print first 10 fibonacci numbers
# 0,1,1,2,3,5,8,13,21,34...
# Used in: mathematics, nature patterns, DSA!
a = 0
b = 1
print("Fibonacci sequence:")
for i in range(21):
print(a, end=" ")
a, b = b, a + b # swap trick!
#this [a, b = b, a + b] is a trick called "Tuple Unpacking", python calculates the entire right side first then assigns the values.
# 0, 1, (0 + 1)1, (1 + 1)2, (2 + 1)3, .... so first two digit would be always a, b then third digit would be c that is ~ a + b = c... b+c = d... etc.
print()
#this print here is used for starting from new line
a, b = 0, 1
for i in range(10):
print(f"{a} + {b} = {a + b}") # This shows the work!
a, b = b, a + b
# ── WHILE LOOP ──────────────────
# while loop = keep going UNTIL condition false
# ── 1. BASIC WHILE ────────────────────────
count = 1
while count <= 5:
print(f"Count: {count}")
count += 1 # without this = infinite loop!
#count += 1 ~ without this it will keep print infinite loop like count: 1;
# ── 2. BREAK ──────────────────────────────
# exit loop completely when condition met
print("Break example:")
count = 0
while True: # infinite loop!
count += 1
print(count)
if count == 5: #Here used comparison operator - equal to, it is keep count till we reach at number 5 (strictly!)
break # stops at 5!
# ── 3. CONTINUE ───────────────────────────
# skip current iteration, keep going
print("Continue example:")
for i in range(10):
if i % 2 == 0: #In this line i represent numbers 1, 2, 3, ... % symbol is modulus that shows remainder.If remainder = 0 it means even number.
continue # skip even numbers
print(i) # only prints odd: 1,3,5,7,9
# ── 4. PASS ───────────────────────────────
# does nothing! placeholder for future code
for i in range(5):
if i == 3:
pass # will add code here later
print(i) # prints all numbers
# ── REAL WORLD EXAMPLES ───────────────────
# 1. ATM MACHINE
balance = 5000
while True:
print(f"\nBalance: {balance}")
amount = int(input("How much to withdraw? "))
if amount > balance:
print("Insufficient balance!")
continue # ask again!
elif amount <= 0:
print("Invalid amount!")
continue
else:
balance -= amount
print(f"Withdrawn! Remaining: {balance}")
another = input("Another withdrawal? (yes/no): ")
if another == "no":
break # exit ATM!
print("Thank you! Goodbye!")
# 2. GUESSING GAME
import random
secret = random.randint(1, 10)
attempts = 0
while True:
guess = int(input("Guess number 1-10: "))
attempts += 1
if guess < secret:
print("Too low!")
elif guess > secret:
print("Too high!")
else:
print(f"Correct! You got it in {attempts} attempts!")
break
rows = 6
for i in range(rows):
for j in range(i):
print(i, end=' ')
print('')
rows = 5
k = 2 * rows - 2
for i in range(rows, -1, -1):
for j in range(k, 0, -1):
print(end=" ")
k = k + 1
for j in range(0, i + 1):
print("*", end=" ")
print("")