0% found this document useful (0 votes)
7 views2 pages

Day2 Advanced Python P

The document outlines advanced Python practice problems for Day 2, including pattern generation, list manipulation, and logic challenges. It provides code snippets for creating butterfly and 0-1 patterns, rotating lists, finding the most frequent character in a string, and checking for Harshad, Automorphic, and Strong numbers. Additionally, it includes methods for identifying prime numbers in a range and summing digits until a single digit is reached.

Uploaded by

Bhavya raval
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views2 pages

Day2 Advanced Python P

The document outlines advanced Python practice problems for Day 2, including pattern generation, list manipulation, and logic challenges. It provides code snippets for creating butterfly and 0-1 patterns, rotating lists, finding the most frequent character in a string, and checking for Harshad, Automorphic, and Strong numbers. Additionally, it includes methods for identifying prime numbers in a range and summing digits until a single digit is reached.

Uploaded by

Bhavya raval
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Day 2: Advanced Python Practice - Questions & Answers

3:00 - 4:00 PM: Pattern Problems

1. Butterfly Pattern
* *
** **
******
** **
* *
Code:
n = 4
for i in range(1, n+1):
print('*'*i + ' '*(2*(n-i)) + '*'*i)
for i in range(n, 0, -1):
print('*'*i + ' '*(2*(n-i)) + '*'*i)

2. 0-1 Triangle
1
0 1
1 0 1
0 1 0 1
Code:
n = 4
for i in range(n):
for j in range(i+1):
print((i+j)%2, end=" ")
print()

4:00 - 5:00 PM: List + String Problems

3. Rotate List Right by k


arr = [1, 2, 3, 4, 5], k = 2 -> [4, 5, 1, 2, 3]
Code:
def rotate_right(arr, k):
k = k % len(arr)
return arr[-k:] + arr[:-k]
print(rotate_right([1,2,3,4,5], 2))

4. Most Frequent Character in String


Code:
from collections import Counter
s = "programming"
freq = Counter(s)
most_common = freq.most_common(1)[0]
print(f"Most frequent: {most_common[0]} ({most_common[1]} times)")

6:00 - 7:00 PM: Logic Challenges


Day 2: Advanced Python Practice - Questions & Answers

5. Harshad Number (Sum of digits divides original number)


Code:
n = 18
s = sum(map(int, str(n)))
print("Harshad" if n % s == 0 else "Not Harshad")

6. Automorphic Number (Square ends with number itself)


Code:
n = 76
print("Automorphic" if str(n*n).endswith(str(n)) else "Not Automorphic")

7. Strong Number (Sum of digit factorials equals number)


Code:
import math
n = 145
s = sum([Link](int(d)) for d in str(n))
print("Strong" if s == n else "Not Strong")

8:00 - 9:30 PM: Range + Revision

8. All Prime Numbers in Range 10-50


Code:
for num in range(10, 51):
if all(num % i != 0 for i in range(2, int(num**0.5)+1)):
print(num, end=" ")

9. Sum of Digits Till One Digit (9875 -> 9)


Code:
n = 9875
while n >= 10:
n = sum(int(d) for d in str(n))
print("Single digit sum:", n)

You might also like