diff options
author | Sudipto Mallick <> | 2024-01-02 03:38:10 +0000 |
---|---|---|
committer | Sudipto Mallick <> | 2024-01-02 03:38:10 +0000 |
commit | 7451edff7692e86c5238ff7bc6659825e242a84e (patch) | |
tree | 830fe25caf403869fe77df83bc44822ea6f5f11d /python | |
parent | a70e0a59817ce06a3dd23b3750ae16ee6660deaf (diff) | |
download | zadania-7451edff7692e86c5238ff7bc6659825e242a84e.tar.gz |
Quick backup, need to be rewritten
Diffstat (limited to 'python')
34 files changed, 1300 insertions, 0 deletions
diff --git a/python/abld b/python/abld new file mode 100755 index 0000000..1cc9d0d --- /dev/null +++ b/python/abld @@ -0,0 +1,4 @@ +#!/bin/sh +d=$(dirname a/$1.pdf) +if [ ! -d $d ]; then mkdir -p $d; fi +typst ${2:-c} --root $PWD text/$1.typ a/$1.pdf diff --git a/python/code/1/1.txt b/python/code/1/1.txt new file mode 100644 index 0000000..827a09a --- /dev/null +++ b/python/code/1/1.txt @@ -0,0 +1,17 @@ +>>> 2 + 3 +5 +>>> 1 + 2 * 3 - 4 +3 +>>> 12 * 5 / 3 * 2 +40.0 +>>> 5 / 8 +0.625 +>>> 23 // 7 +3 +>>> 17 % 5 - 1 +1 +>>> 3 * (4 + 5) + 5 +32 +>>> (2 + (7 + 2) / 3 - 4) / (-2 / 7 + (19 - 3 * 4) / 8) +1.696969696969697 + diff --git a/python/code/1/2.txt b/python/code/1/2.txt new file mode 100644 index 0000000..08c95b5 --- /dev/null +++ b/python/code/1/2.txt @@ -0,0 +1,52 @@ +>>> import math +>>> math.cos(math.pi / 3) +0.5000000000000001 +>>> math.sin(math.pi / 6) +0.49999999999999994 +>>> math.tan(math.pi / 4) +0.9999999999999999 +>>> math.pow(1.123, 0.123) +1.0143707323622344 +>>> math.exp(379) +3.959210944514706e+164 +>>> math.log(10, 2) +3.3219280948873626 +>>> math.hypot(3, 4) +5.0 +>>> math.hypot(3, 4, 5) +7.0710678118654755 +>>> math.degrees(math.pi / 4) +45.0 +>>> math.radians(90) / math.pi +0.5 +>>> [*(print(i, math.sqrt(i)) for i in range(1, 10))] +1 1.0 +2 1.4142135623730951 +3 1.7320508075688772 +4 2.0 +5 2.23606797749979 +6 2.449489742783178 +7 2.6457513110645907 +8 2.8284271247461903 +9 3.0 +[None, None, None, None, None, None, None, None, None]>>> math.modf(12.5) +(0.5, 12.0) +>>> math.gamma(6) +120.0 +>>> [math.floor(x) for x in [12.3, -12.3]] +[12, -13] +>>> [math.ceil(x) for x in [12.3, -12.3]] +[13, -12] +>>> [math.trunc(x) for x in [12.3, -12.3]] +[12, -12] +>>> math.cbrt(8), math.cbrt(10) +(2.0, 2.154434690031884) +>>> math.dist((1, 1), (2, 3)) +2.23606797749979 +>>> math.dist((1, 1), (5, 4)) +5.0 +>>> math.isqrt(23) +4 +>>> math.exp2(10) +1024.0 + diff --git a/python/code/1/3.txt b/python/code/1/3.txt new file mode 100644 index 0000000..90ebdbe --- /dev/null +++ b/python/code/1/3.txt @@ -0,0 +1,17 @@ +>>> a, b = 3 + 2j, 4 - 3j +>>> a + b, a - b, a * b, a / b +((7-1j), (-1+5j), (18-1j), (0.24+0.68j)) +>>> -a, -b +((-3-2j), (-4+3j)) +>>> (a + b) / (a - b) +(-0.4615384615384615-1.3076923076923077j) +>>> abs(a), abs(b) +(3.605551275463989, 5.0) +>>> a.imag, a.real +(2.0, 3.0) +>>> a.conjugate(), b.conjugate() +((3-2j), (4+3j)) +>>> from cmath import phase +>>> phase(a), phase(b) +(0.5880026035475675, -0.6435011087932844) + diff --git a/python/code/1/4.py b/python/code/1/4.py new file mode 100644 index 0000000..7579373 --- /dev/null +++ b/python/code/1/4.py @@ -0,0 +1,8 @@ +from math import gcd +a = int(input('Enter first number: ')) +b = int(input('Enter second number: ')) +print('Sum:', a + b) +print('Product:', a * b) +print('Difference:', a - b) +print('GCD:', gcd(a, b)) + diff --git a/python/code/1/6.py b/python/code/1/6.py new file mode 100644 index 0000000..e3a3cd0 --- /dev/null +++ b/python/code/1/6.py @@ -0,0 +1,4 @@ +a = float(input('Enter first number: ')) +b = float(input('Enter second number: ')) +print('Concatenation of integer parts:', ''.join(str(int(i)) for i in (a, b))) +print('Sum rounded to 2 decimal places:', round(a + b, 2)) diff --git a/python/code/1/7.py b/python/code/1/7.py new file mode 100644 index 0000000..37ecfa3 --- /dev/null +++ b/python/code/1/7.py @@ -0,0 +1,5 @@ +a = int(input('Enter first number: ')) +b = int(input('Enter second number: ')) +q, r = a // b, a % b +q1d, r1d = q % 10, r % 10 +print(f'The one’s place digits of quotient and remainder are {"" if q1d == r1d else "not"} equal.') diff --git a/python/code/2/1.py b/python/code/2/1.py new file mode 100644 index 0000000..7ccd78f --- /dev/null +++ b/python/code/2/1.py @@ -0,0 +1,27 @@ +def gaussian_sum_even(numbers): + assert len(numbers) % 2 == 0 + start_index, end_index = 0, len(numbers) - 1 + sum = 0 + while start_index < end_index: + print(numbers[start_index], '+', numbers[end_index]) + sum += numbers[start_index] + numbers[end_index] + start_index += 1 + end_index -= 1 + print('Result:', sum) + +gaussian_sum_even(list(range(1, 7))) + +def gaussian_sum(numbers): + start_index, end_index = 0, len(numbers) - 1 + sum = 0 + while start_index < end_index: + print(numbers[start_index], '+', numbers[end_index]) + sum += numbers[start_index] + numbers[end_index] + start_index += 1 + end_index -= 1 + middle_value = numbers[len(numbers) // 2] + print(middle_value) + sum += middle_value + print('Result:', sum) + +gaussian_sum(list(range(1, 10))) diff --git a/python/code/2/2.py b/python/code/2/2.py new file mode 100644 index 0000000..bb3e9d2 --- /dev/null +++ b/python/code/2/2.py @@ -0,0 +1,12 @@ +def is_prime(n): + if n < 2: return False + for i in range(2, n): + if n % i == 0: return False + return True + +def filter_prime(numbers): + return [number for number in numbers if is_prime(number)] + +numbers = [int(n) for n in input('Enter a list of numbers: ').split()] +primes = [number for number in numbers if is_prime(number)] +print('List of primes:', primes) diff --git a/python/code/2/3.py b/python/code/2/3.py new file mode 100644 index 0000000..2a8dd10 --- /dev/null +++ b/python/code/2/3.py @@ -0,0 +1,6 @@ +items = [(0, 'd'), (1, 'a'), (2, 'c'), (3, 'b')] + +print(max(items, key=lambda it: it[0])) +print(min(items, key=lambda it: it[1])) +items.sort(key=lambda x: ord(x[1]) - x[0]) +print(items) diff --git a/python/code/2/4.py b/python/code/2/4.py new file mode 100644 index 0000000..21b1bf4 --- /dev/null +++ b/python/code/2/4.py @@ -0,0 +1,27 @@ +from random import randrange + +def game(): + secret = randrange(1, 11) + for i in range(3): + guess = int(input('Enter a guess: ')) + if guess != secret: + print('Wrong guess!', end=' ') + else: + print(f'Correct guess in {i + 1} {"try" if i == 0 else "tries"}!') + return + if i != 2: + print('Try again.') + else: + print('Game over!') + print(f'Secret value was {secret}') + +while True: + print('Welcome to number guessing game') + print('Try to guess the secret random number between 1 to 10 correctly in 3 tries') + game() + choice = input('Do you want to try playing again (y/n, default n): ') + if choice == '' or choice == 'n': + print('Thanks for playing. Bye!') + break + else: + print('Enjoy your next play\n') diff --git a/python/code/2/5.py b/python/code/2/5.py new file mode 100644 index 0000000..ba78eec --- /dev/null +++ b/python/code/2/5.py @@ -0,0 +1,35 @@ +import math, sys +start_day = int(input('Enter the starting day (1 for Sunday, 2 for Monday, ..., 7 for Saturday): ')) +if not (1 <= start_day <= 7): + print('Invalid day, must be between 1 and 7, inclusive') + sys.exit(1) +days_in_month = int(input('Enter the number of days in the month: ')) +print('Calender for this month:') +print(' SUN MON TUE WED THU FRI SAT') +if start_day == 2: + print(' ' * 4, end='') +else: + print(' ' * (4 * (start_day - 1)), end='') + +day = 1 +if start_day != 1: + for i in range(7 - start_day + 1): + print(f' {day:3}', end='') + day += 1 + print() + weeks = math.ceil((days_in_month - (7 - start_day + 1)) / 7) +else: + weeks = math.ceil(days_in_month / 7) + +class StopIt(Exception): pass + +try: + for w in range(weeks): + for i in range(7): + if day > days_in_month: + raise StopIt() + print(f' {day:3}', end='') + day += 1 + print() +except StopIt: pass +print() diff --git a/python/code/2/6.py b/python/code/2/6.py new file mode 100644 index 0000000..8013ae4 --- /dev/null +++ b/python/code/2/6.py @@ -0,0 +1,11 @@ +line = input('Enter a string: ') +uppercase_count, lowercase_count, digit_count = 0, 0, 0 +for c in line: + if c.isupper(): uppercase_count += 1 + elif c.islower(): lowercase_count += 1 + elif c.isdigit(): digit_count += 1 + +print('Number of') +print(' Uppercase characters:', uppercase_count) +print(' Lowercase characters:', lowercase_count) +print(' Digits:', digit_count) diff --git a/python/code/2/7.py b/python/code/2/7.py new file mode 100644 index 0000000..3212851 --- /dev/null +++ b/python/code/2/7.py @@ -0,0 +1,9 @@ +size = int(input('Enter pattern size: ')) +if size < 0: + print('Invalid size') +else: + print('*' * size) + for i in range(size - 2): + print('*' + ' ' * (size - 2) + '*') + if size > 1: + print('*' * size) diff --git a/python/code/3/3.py b/python/code/3/3.py new file mode 100644 index 0000000..7f9a98f --- /dev/null +++ b/python/code/3/3.py @@ -0,0 +1,11 @@ +full_name = input("Enter your full name: ") +space_index = full_name.rfind(' ') + +if space_index == -1: + print(f"Your name in reverse: {full_name}") +else: + first_name = full_name[:full_name.index(' ')] + last_name = full_name[space_index + 1:] + print(f"Your name in reverse: {last_name}, {first_name}") + + diff --git a/python/code/3/4.py b/python/code/3/4.py new file mode 100644 index 0000000..3c0a566 --- /dev/null +++ b/python/code/3/4.py @@ -0,0 +1,25 @@ +def our_str_count(s, sub, start=None, end=None): + ss = s[slice(start, end)] + if len(sub) == 0: return len(ss) + 1 + elif len(ss) < len(sub): return 0 + i, l, ls = 0, len(ss), len(sub) + count = 0 + while i < l: + if ss[i:i+ls] == sub: + count += 1 + i += ls + else: + i += 1 + return count + +s = input('Enter a string: ') +sub = input('Enter substring to count: ') +idcs = input('Enter starting and ending indices (default: whole string): ') +if len(idcs) == 0: + idcs = (None, None) +else: + idcs = (int(i) for i in idcs.split()) +start, end = idcs + +print('Count:', our_str_count(s, sub, start, end)) + diff --git a/python/code/3/5.py b/python/code/3/5.py new file mode 100644 index 0000000..7db13a0 --- /dev/null +++ b/python/code/3/5.py @@ -0,0 +1,5 @@ +text = input('Enter a line of text: ') +vowels = set('aeiouAEIOU') +ctext = ''.join(c for c in text if c not in vowels) +print('Given text with all vowels removed:', ctext) + diff --git a/python/code/3/6.py b/python/code/3/6.py new file mode 100644 index 0000000..f9b4b99 --- /dev/null +++ b/python/code/3/6.py @@ -0,0 +1,27 @@ +import sys +message = input('Enter message: ') +key = None +try: + key = int(input('Enter key (1-25, -1 - -25): ')) +except ValueError: + print('Invalid key, must be an integer') + sys.exit(1) + +if key < 0: key += 26 +if not (1 <= key <= 25): + print('Invalid key, must be an integer with absolute value between 1 and 25, inclusive') + sys.exit(2) + +marr = bytearray(message, 'ascii') +for i in range(len(marr)): + c = marr[i] + if 64 < c < 64 + 27: + c = ((c - 65) + key) % 26 + 65 + elif 96 < c < 96 + 27: + c = ((c - 97) + key) % 26 + 97 + marr[i] = c + +print('Ceesar cipher with k =', key, 'applied to the given message:') +print(str(marr, encoding='ascii')) + + diff --git a/python/code/3/7.py b/python/code/3/7.py new file mode 100644 index 0000000..7a17f08 --- /dev/null +++ b/python/code/3/7.py @@ -0,0 +1,9 @@ +import sys +address = input('Enter an E-mail address: ') +if '@' not in address: + print(address, 'is not a valid E-mail address') + sys.exit(1) +at_position = address.rindex('@') +username, domain = address[:at_position], address[at_position+1:] +print('Username:', username) +print('Domain:', domain) diff --git a/python/index.typ b/python/index.typ new file mode 100644 index 0000000..98accaf --- /dev/null +++ b/python/index.typ @@ -0,0 +1,224 @@ +#import "tpl.typ": * +#show: body => apply(body) +#show heading: set text(font: "New Computer Modern Sans", weight: 450) +#let semibold(body) = text(weight: 600, body) +#let roman(body) = text(weight: 400, body) +// #text(size: 1.3em, font: "New Computer Modern Sans", align(center)[#semibold[Practical Assignments] \ _on_ \ #semibold[Programming in Python Lab]]) +#text(size: 1.3em, font: "New Computer Modern Sans", align(center)[#semibold[Programming in Python Lab]]) +#align(center, text(size: 1.1em)[DSE-B-1 \ Use of user defined functions are encouraged wherever applicable]) +#set enum(full: true, numbering: (..args) => { + let patterns = ("1.", "(a)") + let pattern = patterns.at(calc.min(args.pos().len(), patterns.len()) - 1) + numbering(pattern, args.pos().last()) +}) +#let nntiv = body => [ + #set text(size: 0.9em, fill: rgb("#777777")) + \ #body +] + +#set raw(lang: "python") +#set par(justify: true) +#set heading(numbering: "1 ") + +#heading(numbering: none)[0 #h(0.25em) Definitions] +- _Interactive interpreter_ allows the user to run one or more lines of code interactively, immediately showing the result and output of code. +- _Programs_ are to be written and saved in text files, usually with the file extension `.py`, and run using the Python executable from CLI (Command Prompt or Powershell in Windows, shell in Linux etc.) or the inbuilt IDLE. + += Basic Python Syntax + ++ Using the Python _interactive interpreter_ as a basic calculator, demonstrate the order of operations using parentheses for arithmetic operations. ++ Using the Python _interactive interpreter_, demonstrate at least 15 functions in the math module. + #nntiv[For example, to find the GCD of two numbers, area and perimeter of circle using `math.pi` etc.] ++ Using the Python _interactive interpreter_, demonstrate the use of complex numbers and their operations on them. + #nntiv[For example, in finding the modulus and phase angle (amplitude or argument) of a complex number.] ++ Write a Python program to take two numbers from the user and show their the sum, product, difference and the GCD. ++ Write a Python program to demonstrate the use of _regular expressions_ using `re.split`, `re.join`, `re.search` and `re.match` methods from `re` module. ++ Write a program to take two floating point numbers as input from the user. Concatenate the integral parts of the two numbers and display them, also display the sum of the input floating point numbers rounded upto 2 decimal places. ++ Write a program to divide two numbers and check if the digits at one’s place of the quotient and remainder are equal. + += Decision Control Statements + ++ Write a Python program to calculate the sum a list of number of (i) even length and then (ii) any length, using `while` loop: at each step, add $k$th number from the start and end of the list and display it, for $k$ from $0$ to half the length the list. + #nntiv[For example: if the list is `[1, 2, 3, 4, 5, 6]`, the program should output `1 + 6`, `2 + 5`, and `3 + 4` in separate lines, and the result of the addition `21`.] ++ Write a Python program to sperate prime numbers from a given list of numbers and store them in another list. ++ Write a Python program to demonstrate keyword argument `key` of `sum()`, `min()`, `max()`, and `sort()` functions using `lambda`s. ++ Write a Python program to design the following random number guessing game. A random number from 1 to 10 (inclusive) is selected as a secret number, then the user is prompted to guess the number. If the user’s guess matches the secret number, the game ends with an appropriate message, otherwise the user is asked to try again. After 3 unsuccessful guesses, the user loses and the game ends with an appropriate message revealing the secret number. At the end of the game, ask the user whether to play again. (Use the `random` module to obtain the secret number.) ++ Write a Python program to generate the calendar of a month given the start day (`1` for Sunday, `2` for Monday, ... `7` is Saturday) and the number of days in the month. + #nntiv[An example:] + #[ + #show raw: block.with(inset: 0pt, fill: white, width: 20em, outset: 0pt) + #show: it => align(center, it) + ```text + Enter the starting day: 5 + Enter the number of days in the month: 30 + Calender for this month: + SUN MON TUE WED THU FRI SAT + 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 + ```] ++ Write a Python program to take input from user as a string and count the lowercase characters, uppercase characters and digits in it. ++ #table( + columns: (1fr, 4em), stroke: none, + inset: 0pt, gutter: 0pt, + column-gutter: 5pt, + [Write a Python program to print the following star pattern of the given size. \ + #nntiv[Example (with size 4):]], + [ + #set par(leading: 0em) + #set block(inset: 0pt, width: 4em, outset: 0pt) + ```text +**** +* * +* * +**** +``` + ]) + += Strings + ++ Write a Python program to ask the user for two strings, print a new string where the first string is reversed, and the second string is converted to upper case, only using string slicing and `+` operator. + #nntiv[Sample strings: `Pets`, `party`, output: `steP PARTY`.] ++ Write a Python program to from a list of words, join all the words in the odd and even indices to form two strings using list slicing and `join` method. + #nntiv[Example: for a list `['abc', 'def', 'ghi', 'jkl', 'mno', 'pqrs', 'tuv', 'wxyz']`, the result are: `'abcghimnotuv'` and `'defjklpqrswxyz'`.] ++ #text(fill: red, sym.convolve) + Write a Python program to input your name in a string, separate the first name and last name using the slice operator with negative indexing, and display your name in reverse using a formatted string. ++ Write a Python program to implement a function that works like `str.count()` method. ++ Write a Python program to take input a string and display it after removing all the vowels. ++ Write a Python program that take a plain text string and generate cyphertext by using $k$th next character with wrap around for each character for a given constant key value $k$. + #nntiv[Also known as _Caesar Cipher_. \ Example: for key $k = 23$, the message `"The quick brown fox jumps over the lazy dog."` encrypts to `"Qeb nrfzh yoltk clu grjmp lsbo qeb ixwv ald."`.] ++ Write a Python program to sperate the username and domain name of an email address. +#{ /* ++ Write a Python program to separate the `local-part` and `domain` of an email address given as per RFC 5322 format for `addr-spec` (§3.4.1): + ```text + addr-spec = local-part "@" domain + local-part = dot-atom / quoted-string / obs-local-part + domain = dot-atom / domain-literal / obs-domain + dtext = %d33-90 / ; Printable US-ASCII + %d94-126 / ; characters not including + obs-dtext ; "[", "]", or "\" + FWS = ([*WSP CRLF] 1*WSP) / obs-FW + ; Folding white space + ctext = %d33-39 / ; Printable US-ASCII + %d42-91 / ; characters not including + %d93-126 / ; "(", ")", or "\" + obs-ctext + ccontent = ctext / quoted-pair / comment + comment = "(" *([FWS] ccontent) [FWS] ")" + CFWS = (1*([FWS] comment) [FWS]) / FWS + atext = ALPHA / DIGIT / ; Printable US-ASCII + "!" / "#" / ; characters not including + "$" / "%" / ; specials. Used for atoms. + "&" / "'" / + "*" / "+" / + "-" / "/" / + "=" / "?" / + "^" / "_" / + "`" / "{" / + "|" / "}" / + "~" + atom = [CFWS] 1*atext [CFWS] + dot-atom-text = 1*atext *("." 1*atext) + dot-atom = [CFWS] dot-atom-text [CFWS] + qtext = %d33 / ; Printable US-ASCII + %d35-91 / ; characters not including + %d93-126 / ; "\" or the quote character + obs-qtext + quoted-pair = ("\" (VCHAR / WSP)) / obs-qp + obs-qp = "\" (%d0 / obs-NO-WS-CTL / LF / CR) + qcontent = qtext / quoted-pair + quoted-string = [CFWS] + DQUOTE *([FWS] qcontent) [FWS] DQUOTE + [CFWS] + word = atom / quoted-string + obs-local-part = word *("." word) + obs-domain = atom *("." atom) + obs-dtext = obs-NO-WS-CTL / quoted-pair + obs-NO-WS-CTL = %d1-8 / ; US-ASCII control + %d11 / ; characters that do not + %d12 / ; include the carriage + %d14-31 / ; return, line feed, and + %d127 ; white space characters + ``` +*/} + += Lists + ++ Write a Python program to simulate a stack and a queue using lists. + #nntiv[Note that the queue deletion operation won’t run in $O(1)$ time.] ++ Write a Python program to find all the odd numbers and numbers divisible by 3 from a list of numbers using list comprehension. ++ Write a Python program to find the second largest number in a list of numbers. ++ Write a Python program to generate a list of numbers `items` using list comprehension as per the following description: + $ "items"[ i ] = cases( + i^2 quad "if " i "is even", + i^3 quad "if " i "is odd", + ) $ + where $i$ denotes the index of the list. ++ Write a Python program to use the `map()` method to square each number in a list. ++ Write a Python program to remove all duplicate items in a list. + += Tuples + ++ Write a Python program that, given sequences of first and last names, generate a tuple of tuples each of which contains the full name of a person, using the `zip()` function. ++ Write a Python program to create and modify tuples in various ways. ++ Write a Python program to take a nested tuple with the name, Roll No. and marks of some students and find the name of the student having highest marks. ++ Write a Python program to take a list of numbers (both positive and negative) and make a new tuple out from this list with only the positive values. ++ Write a Python program to demonstrate the definition and use of a function taking variable length arguments (scatter and gather) using the `*` symbol. + += Sets + ++ Write a Python program to remove duplicate items of a list of numbers and duplicate characters in a string using sets. ++ Write a Python program to implement the Jaccard and cosine similarity of two sets using the set union and intersection operations. ++ Write a Python program to show the difference between `set.remove()` and `set.discard()` methods. ++ Write a Python program that creates two sets of even numbers in the range 1 to 10 and composite numbers in the range 1 to 20 to demonstrate the use of `all()`, `issuperset()`, `len()`, `sum()` methods on the sets. ++ Write a Python program that generates a set of prime numbers and another set of odd numbers. Demonstrate the result of union, intersection, difference, symmetric difference operations on these sets. ++ Write a Python program that creates two sets: squares and cubes in the range 1 to 10 to demonstrate the use of `update()`, `pop()`, `remove()`, `add()` and `clear()` methods. ++ Write a Python program to demonstrate the use of frozensets. + += Dictionaries + ++ Write a Python program to count the word and letter occurrences in a long string of text using dictionaries. ++ Write a Python program to invert a dictionary such the previous keys become values and values become keys. + #nntiv[For example: if the initial and inverted dictionaries are `d1` and `d2`, where `d1 = {1: 'a', 2: 'b', 3: 120}`, then `d2 = {'a': 1, 2: 'b', 120: 3}`.] ++ What if the values in 2 are not immutable? Use frozensets, For repeated values, use lists. For example: if `d1 = {1: 'a', 2: 'a', 4: [1, 2]}`, then `d2 = {'a': [1, 2], frozenset([1, 2]): 4}`. ++ Write a Python program with a function to generate the Fibonacci numbers in + + Exponential time using the naïve algorithm. + + Linear time using dynamic programming (memoization) with a dictionary. ++ Write a Python program to generate a dictionary where each key is a number and the corresponding value is the number’s square using dictionary comprehensions. ++ Write a Python program to store a sparse matrix as a dictionary. ++ Write a Python program that combines lists of keys and values into a dictionary. + += User-Defined Functions + ++ Write a Python program to implement quick sort and merge sort algorithms to sort lists of numbers. ++ Write a Python program that displays the Pascal’s triangle of a given size. ++ Three positive integers $a$, $b$, and $c$ are Pythagorean triples if $a^2 + b^2 = c^2$. Write a Python program with a function to generate all Pythagorean triples in a certain range. ++ Write two functions that simulate the toss of a fair coin, and the roll of an unbiased $n$ sided die using the `random` module. ++ Modify the previous assignment. Now the coin and the die are not fair, with each outcome having a given probability. + += File Handling; `sys`, `pickle` and `csv` modules + ++ Basic file operations. Explore the different file modes. ++ Emulate the unix `cp`, `grep`, `cat` programs in Python. In each case, the user should pass the arguments to the program as command line arguments. ++ Use pickle for persistent storage of variables. + += Object Oriented Programming + ++ Create a `Graph` class to store and manipulate graphs. It should have the following functions: + + Read an edge list file, where each edge `(u, v)` appears exactly once in the file as space separated values. + + Add and remove nodes and edges. + + Print nodes, and edges in a user readable format. + + Computes basic statistics of the graph like degree distribution, clustering coefficient, and the number of connected components. + + Finding all the neighbors of a node. + + Finding all the connected components and storing them as individual Graph objects inside the class. + + Finding single source shortest paths using Breadth First Search. ++ Make a `DiGraph` class to handle directed graphs which inherits from the `Graph` class. In addition to all of the functionalities of 1, it should support the following operations + + Finding the predecessors and successors of a node. + + Creating a new `DiGraph` object where all the edges are reversed. + + Finding the strongly connected components. ++ Extend 1 and 2 to handle weighted graphs, and implement Dijkstra’s and Floyd-Warshall algorithms to compute the single source and all pairs shortest paths. ++ Use the graph containers in 1, 2, and 3 to implement additional graph algorithms. + + diff --git a/python/output/1/6.txt b/python/output/1/6.txt new file mode 100644 index 0000000..b238b5f --- /dev/null +++ b/python/output/1/6.txt @@ -0,0 +1,7 @@ +``` +Enter first number: 12.345 +Enter second number: 678.9999 +Concatenation of integer parts: 12678 +Sum rounded to 2 decimal places: 691.34 +``` + diff --git a/python/output/2/1.txt b/python/output/2/1.txt new file mode 100644 index 0000000..81480d1 --- /dev/null +++ b/python/output/2/1.txt @@ -0,0 +1,12 @@ +``` +1 + 6 +2 + 5 +3 + 4 +Result: 21 +1 + 9 +2 + 8 +3 + 7 +4 + 6 +5 +Result: 45 +``` \ No newline at end of file diff --git a/python/output/2/2.txt b/python/output/2/2.txt new file mode 100644 index 0000000..4c06a26 --- /dev/null +++ b/python/output/2/2.txt @@ -0,0 +1,4 @@ +``` +Enter a list of numbers: 1 42 73 23 13 15 17 9 65 48 37 11 4 +List of primes: [73, 23, 13, 17, 37, 11] +``` diff --git a/python/output/2/4.txt b/python/output/2/4.txt new file mode 100644 index 0000000..8188169 --- /dev/null +++ b/python/output/2/4.txt @@ -0,0 +1,24 @@ +``` +Welcome to number guessing game +Try to guess the secret random number between 1 to 10 correctly in 3 tries +Enter a guess: 10 +Wrong guess! Try again. +Enter a guess: 8 +Wrong guess! Try again. +Enter a guess: 3 +Wrong guess! Game over! +Secret value was 5 +Do you want to try playing again (y/n, default n): y +Enjoy your next play + +Welcome to number guessing game +Try to guess the secret random number between 1 to 10 correctly in 3 tries +Enter a guess: 8 +Wrong guess! Try again. +Enter a guess: 3 +Wrong guess! Try again. +Enter a guess: 4 +Correct guess in 3 tries! +Do you want to try playing again (y/n, default n): +Thanks for playing. Bye! +``` diff --git a/python/output/2/5.txt b/python/output/2/5.txt new file mode 100644 index 0000000..35b538d --- /dev/null +++ b/python/output/2/5.txt @@ -0,0 +1,34 @@ +``` +Enter the starting day (1 for Sunday, 2 for Monday, ..., 7 for Saturday): 1 +Enter the number of days in the month: 30 +Calender for this month: + SUN MON TUE WED THU FRI SAT + 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 +``` +``` +Enter the starting day (1 for Sunday, 2 for Monday, ..., 7 for Saturday): 2 +Enter the number of days in the month: 28 +Calender for this month: + SUN MON TUE WED THU FRI SAT + 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 +``` +``` +Enter the starting day (1 for Sunday, 2 for Monday, ..., 7 for Saturday): 7 +Enter the number of days in the month: 31 +Calender for this month: + SUN MON TUE WED THU FRI SAT + 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 +``` diff --git a/python/output/2/6.txt b/python/output/2/6.txt new file mode 100644 index 0000000..32f6729 --- /dev/null +++ b/python/output/2/6.txt @@ -0,0 +1,7 @@ +``` +Enter a string: Hello World ABCDE 123456 +Number of + Uppercase characters: 7 + Lowercase characters: 8 + Digits: 6 +``` \ No newline at end of file diff --git a/python/output/2/7.txt b/python/output/2/7.txt new file mode 100644 index 0000000..abb9c92 --- /dev/null +++ b/python/output/2/7.txt @@ -0,0 +1,24 @@ +``` +Enter pattern size: 4 +**** +* * +* * +**** +``` +``` +Enter pattern size: 5 +***** +* * +* * +* * +***** +``` +``` +Enter pattern size: 6 +****** +* * +* * +* * +* * +****** +``` diff --git a/python/output/3/3.txt b/python/output/3/3.txt new file mode 100644 index 0000000..e2163f7 --- /dev/null +++ b/python/output/3/3.txt @@ -0,0 +1,9 @@ +``` +Enter your full name: Sudipto Mallick +Your name in reverse: Mallick, Sudipto +``` +``` +Enter your full name: Ayush Kumar Shukla +Your name in reverse: Shukla, Ayush +``` + diff --git a/python/output/3/5.txt b/python/output/3/5.txt new file mode 100644 index 0000000..11fdc1d --- /dev/null +++ b/python/output/3/5.txt @@ -0,0 +1,4 @@ +``` +Enter a line of text: The trend towards remote work has accelerated in recent years, with many companies adopting a hybrid or fully remote model. It offers employees more flexibility and can lead to increased productivity and job satisfaction. +Given text with all vowels removed: Th trnd twrds rmt wrk hs cclrtd n rcnt yrs, wth mny cmpns dptng hybrd r flly rmt mdl. t ffrs mplys mr flxblty nd cn ld t ncrsd prdctvty nd jb stsfctn. +``` diff --git a/python/output/3/6.txt b/python/output/3/6.txt new file mode 100644 index 0000000..ba5b336 --- /dev/null +++ b/python/output/3/6.txt @@ -0,0 +1,12 @@ +``` +Enter message: A quick brown fox jumps over a lazy dog. +Enter key (1-25, -1 - -25): 5 +Ceesar cipher with k = 5 applied to the given message: +F vznhp gwtbs ktc ozrux tajw f qfed itl. +``` +``` +Enter message: F vznhp gwtbs ktc ozrux tajw f qfed itl. +Enter key (1-25, -1 - -25): -5 +Ceesar cipher with k = 21 applied to the given message: +A quick brown fox jumps over a lazy dog. +``` diff --git a/python/output/3/7.txt b/python/output/3/7.txt new file mode 100644 index 0000000..23b872f --- /dev/null +++ b/python/output/3/7.txt @@ -0,0 +1,9 @@ +``` +Enter an E-mail address: user@example.net +Username: user +Domain: example.net +``` +``` +Enter an E-mail address: abcdefghi +abcdefghi is not a valid E-mail address +``` diff --git a/python/sbld b/python/sbld new file mode 100755 index 0000000..cea7430 --- /dev/null +++ b/python/sbld @@ -0,0 +1,2 @@ +#!/bin/sh +typst ${2:-c} --root $PWD text/s$1.typ a/s$1.pdf diff --git a/python/tpl.typ b/python/tpl.typ new file mode 100644 index 0000000..657176d --- /dev/null +++ b/python/tpl.typ @@ -0,0 +1,51 @@ +#import "@preview/codelst:1.0.0": sourcefile +#let hlfile(filename) = sourcefile(read(filename), file: filename) +#let apply(body) = { +// let body-font-settings = (font: "Nunito Sans 10pt", size: 12pt, stretch: 75%) +// let body-font-settings = (font: "Hanken Grotesk", size: 12pt, stretch: 75%) +// let body-font-settings = (font: "Lato", size: 11pt) + let body-font-settings = (font: "New Computer Modern", size: 12pt, weight: 450) + let page-margin = (left: 0.75in, right: 0.25in, top: 0.35in, bottom: 0.15in) + let margin = (left: 0.75in, right: 0.25in, top: 2em, bottom: 2em) + let page-frame-thickness = 1.5pt + set page( + margin: (..page-margin, bottom: margin.bottom + 2em), + numbering: "1", + background: align(top + start, pad(..margin, rect(width: 100%, height: 100%, stroke: page-frame-thickness + gray))), + footer: locate(loc => align(center, move(dy: -margin.bottom + 1em, text(..body-font-settings, size: 9pt, counter(page).display(loc.page-numbering()))))) + ) + show: block.with(breakable: true, width: 100%, inset: page-frame-thickness + 1em) + + set text(..body-font-settings) + + let code-color = rgb("#f4f4f4") +// show raw: set text(font: "CommitMono", size: 1.1em) +// show raw: set text(font: "Inconsolata", size: 1.1em) +// show raw: set text(font: "Source Code Pro", size: 1.1em) +// show raw: set text(font: "Iosevka Fixed", size: 1.1em) + show raw: set text(font: "New Computer Modern Mono", size: 1.2em) + show raw.where(block: false): box.with( + fill: code-color, + inset: (x: 4pt, y: 0pt), + outset: (y: 4pt), + radius: 2pt, + ) + show raw.where(block: true): block.with( + fill: code-color, + inset: 10pt, + radius: 4pt, + width: 100%, + ) + show raw.where(block: true): it => align(left, it) + set raw(theme: "vendor/gr.tmTheme") + set par(leading: 0.5em) + body +} + +#let assignment(number) = { + set align(center) + [== Assignment #number] +} +#let objective(body) = align(center, [*Objective*: #body]) +#let oset(kind) = block(spacing: 0.6em, [===== #h(1em) #kind]) + diff --git a/python/vendor/gr.tmTheme b/python/vendor/gr.tmTheme new file mode 100644 index 0000000..c4ab8f1 --- /dev/null +++ b/python/vendor/gr.tmTheme @@ -0,0 +1,566 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> +<plist version="1.0"> +<dict> + <key>author</key> + <string>Template: Chris Kempson, Scheme: Alexandre Gavioli (https://github.com/Alexx2/)</string> + <key>name</key> + <string>Base16 Grayscale Light</string> + <key>semanticClass</key> + <string>theme.base16.grayscale-light</string> + <key>colorSpaceName</key> + <string>sRGB</string> + <key>gutterSettings</key> + <dict> + <key>background</key> + <string>#e3e3e3</string> + <key>divider</key> + <string>#e3e3e3</string> + <key>foreground</key> + <string>#ababab</string> + <key>selectionBackground</key> + <string>#b9b9b9</string> + <key>selectionForeground</key> + <string>#525252</string> + </dict> + <key>settings</key> + <array> + <dict> + <key>settings</key> + <dict> + <key>background</key> + <string>#f7f7f7</string> + <key>caret</key> + <string>#464646</string> + <key>foreground</key> + <string>#464646</string> + <key>invisibles</key> + <string>#ababab</string> + <key>lineHighlight</key> + <string>#ababab55</string> + <key>selection</key> + <string>#b9b9b9</string> + </dict> + </dict> + <dict> + <key>name</key> + <string>Text</string> + <key>scope</key> + <string>variable.parameter.function</string> + <key>settings</key> + <dict> + <key>foreground</key> + <string>#222222</string> + </dict> + </dict> + <dict> + <key>name</key> + <string>Comments</string> + <key>scope</key> + <string>comment, punctuation.definition.comment</string> + <key>settings</key> + <dict> + <key>foreground</key> + <string>#777777</string> + <key>fontStyle</key> + <string>bold</string> + </dict> + </dict> + <dict> + <key>name</key> + <string>Punctuation</string> + <key>scope</key> + <string>punctuation.definition.string, punctuation.definition.variable, punctuation.definition.string, punctuation.definition.parameters, punctuation.definition.string, punctuation.definition.array</string> + <key>settings</key> + <dict> + <key>foreground</key> + <string>#111111</string> + </dict> + </dict> + <dict> + <key>name</key> + <string>Delimiters</string> + <key>scope</key> + <string>none</string> + <key>settings</key> + <dict> + <key>foreground</key> + <string>#222222</string> + </dict> + </dict> + <dict> + <key>name</key> + <string>Operators</string> + <key>scope</key> + <string>keyword.operator</string> + <key>settings</key> + <dict> + <key>foreground</key> + <string>#222222</string> + </dict> + </dict> + <dict> + <key>name</key> + <string>Keywords</string> + <key>scope</key> + <string>keyword</string> + <key>settings</key> + <dict> + <key>foreground</key> + <string>#333333</string> + <key>fontStyle</key> + <string>bold</string> + </dict> + </dict> + <dict> + <key>name</key> + <string>Variables</string> + <key>scope</key> + <string>variable</string> + <key>settings</key> + <dict> + <key>foreground</key> + <string>#555555</string> + </dict> + </dict> + <dict> + <key>name</key> + <string>Functions</string> + <key>scope</key> + <string>entity.name.function, meta.require, support.function.any-method, variable.function, variable.annotation, support.macro</string> + <key>settings</key> + <dict> + <key>foreground</key> + <string>#444444</string> + </dict> + </dict> + <dict> + <key>name</key> + <string>Labels</string> + <key>scope</key> + <string>entity.name.label</string> + <key>settings</key> + <dict> + <key>foreground</key> + <string>#5e5e5e</string> + </dict> + </dict> + <dict> + <key>name</key> + <string>Classes</string> + <key>scope</key> + <string>support.class, entity.name.class, entity.name.type.class</string> + <key>settings</key> + <dict> + <key>foreground</key> + <string>#666666</string> + </dict> + </dict> + <dict> + <key>name</key> + <string>Classes</string> + <key>scope</key> + <string>meta.class</string> + <key>settings</key> + <dict> + <key>foreground</key> + <string>#101010</string> + </dict> + </dict> + <dict> + <key>name</key> + <string>Methods</string> + <key>scope</key> + <string>keyword.other.special-method</string> + <key>settings</key> + <dict> + <key>foreground</key> + <string>#444444</string> + </dict> + </dict> + <dict> + <key>name</key> + <string>Storage</string> + <key>scope</key> + <string>storage</string> + <key>settings</key> + <dict> + <key>foreground</key> + <string>#444444</string> + <key>fontStyle</key> + <string>bold</string> + </dict> + </dict> + <dict> + <key>name</key> + <string>Support</string> + <key>scope</key> + <string>support.function</string> + <key>settings</key> + <dict> + <key>foreground</key> + <string>#868686</string> + </dict> + </dict> + <dict> + <key>name</key> + <string>Strings, Inherited Class</string> + <key>scope</key> + <string>string, constant.other.symbol, entity.other.inherited-class</string> + <key>settings</key> + <dict> + <key>foreground</key> + <string>#333333</string> + </dict> + </dict> + <dict> + <key>name</key> + <string>Integers</string> + <key>scope</key> + <string>constant.numeric</string> + <key>settings</key> + <dict> + <key>foreground</key> + <string>#000000</string> + </dict> + </dict> + <dict> + <key>name</key> + <string>Floats</string> + <key>scope</key> + <string>none</string> + <key>settings</key> + <dict> + <key>foreground</key> + <string>#000000</string> + </dict> + </dict> + <dict> + <key>name</key> + <string>Boolean</string> + <key>scope</key> + <string>none</string> + <key>settings</key> + <dict> + <key>foreground</key> + <string>#222222</string> + </dict> + </dict> + <dict> + <key>name</key> + <string>Constants</string> + <key>scope</key> + <string>constant</string> + <key>settings</key> + <dict> + <key>foreground</key> + <string>#000000</string> + </dict> + </dict> + <dict> + <key>name</key> + <string>Tags</string> + <key>scope</key> + <string>entity.name.tag</string> + <key>settings</key> + <dict> + <key>foreground</key> + <string>#7c7c7c</string> + </dict> + </dict> + <dict> + <key>name</key> + <string>Attributes</string> + <key>scope</key> + <string>entity.other.attribute-name</string> + <key>settings</key> + <dict> + <key>foreground</key> + <string>#999999</string> + </dict> + </dict> + <dict> + <key>name</key> + <string>Attribute IDs</string> + <key>scope</key> + <string>entity.other.attribute-name.id, punctuation.definition.entity</string> + <key>settings</key> + <dict> + <key>foreground</key> + <string>#686868</string> + </dict> + </dict> + <dict> + <key>name</key> + <string>Selector</string> + <key>scope</key> + <string>meta.selector</string> + <key>settings</key> + <dict> + <key>foreground</key> + <string>#747474</string> + </dict> + </dict> + <dict> + <key>name</key> + <string>Values</string> + <key>scope</key> + <string>none</string> + <key>settings</key> + <dict> + <key>foreground</key> + <string>#999999</string> + </dict> + </dict> + <dict> + <key>name</key> + <string>Headings</string> + <key>scope</key> + <string>markup.heading punctuation.definition.heading, entity.name.section</string> + <key>settings</key> + <dict> + <key>fontStyle</key> + <string></string> + <key>foreground</key> + <string>#686868</string> + </dict> + </dict> + <dict> + <key>name</key> + <string>Units</string> + <key>scope</key> + <string>keyword.other.unit</string> + <key>settings</key> + <dict> + <key>foreground</key> + <string>#999999</string> + </dict> + </dict> + <dict> + <key>name</key> + <string>Bold</string> + <key>scope</key> + <string>markup.bold, punctuation.definition.bold</string> + <key>settings</key> + <dict> + <key>fontStyle</key> + <string>bold</string> + <key>foreground</key> + <string>#a0a0a0</string> + </dict> + </dict> + <dict> + <key>name</key> + <string>Italic</string> + <key>scope</key> + <string>markup.italic, punctuation.definition.italic</string> + <key>settings</key> + <dict> + <key>fontStyle</key> + <string>italic</string> + <key>foreground</key> + <string>#747474</string> + </dict> + </dict> + <dict> + <key>name</key> + <string>Code</string> + <key>scope</key> + <string>markup.raw.inline</string> + <key>settings</key> + <dict> + <key>foreground</key> + <string>#8e8e8e</string> + </dict> + </dict> + <dict> + <key>name</key> + <string>Link Text</string> + <key>scope</key> + <string>string.other.link, punctuation.definition.string.end.markdown, punctuation.definition.string.begin.markdown</string> + <key>settings</key> + <dict> + <key>foreground</key> + <string>#7c7c7c</string> + </dict> + </dict> + <dict> + <key>name</key> + <string>Link Url</string> + <key>scope</key> + <string>meta.link</string> + <key>settings</key> + <dict> + <key>foreground</key> + <string>#999999</string> + </dict> + </dict> + <dict> + <key>name</key> + <string>Lists</string> + <key>scope</key> + <string>markup.list</string> + <key>settings</key> + <dict> + <key>foreground</key> + <string>#7c7c7c</string> + </dict> + </dict> + <dict> + <key>name</key> + <string>Quotes</string> + <key>scope</key> + <string>markup.quote</string> + <key>settings</key> + <dict> + <key>foreground</key> + <string>#999999</string> + </dict> + </dict> + <dict> + <key>name</key> + <string>Separator</string> + <key>scope</key> + <string>meta.separator</string> + <key>settings</key> + <dict> + <key>background</key> + <string>#b9b9b9</string> + <key>foreground</key> + <string>#464646</string> + </dict> + </dict> + <dict> + <key>name</key> + <string>Inserted</string> + <key>scope</key> + <string>markup.inserted</string> + <key>settings</key> + <dict> + <key>foreground</key> + <string>#8e8e8e</string> + </dict> + </dict> + <dict> + <key>name</key> + <string>Deleted</string> + <key>scope</key> + <string>markup.deleted</string> + <key>settings</key> + <dict> + <key>foreground</key> + <string>#7c7c7c</string> + </dict> + </dict> + <dict> + <key>name</key> + <string>Changed</string> + <key>scope</key> + <string>markup.changed</string> + <key>settings</key> + <dict> + <key>foreground</key> + <string>#747474</string> + </dict> + </dict> + <dict> + <key>name</key> + <string>Colors</string> + <key>scope</key> + <string>constant.other.color</string> + <key>settings</key> + <dict> + <key>foreground</key> + <string>#868686</string> + </dict> + </dict> + <dict> + <key>name</key> + <string>Regular Expressions</string> + <key>scope</key> + <string>string.regexp</string> + <key>settings</key> + <dict> + <key>foreground</key> + <string>#868686</string> + </dict> + </dict> + <dict> + <key>name</key> + <string>Escape Characters</string> + <key>scope</key> + <string>constant.character.escape</string> + <key>settings</key> + <dict> + <key>foreground</key> + <string>#868686</string> + </dict> + </dict> + <dict> + <key>name</key> + <string>Embedded</string> + <key>scope</key> + <string>punctuation.section.embedded, variable.interpolation</string> + <key>settings</key> + <dict> + <key>foreground</key> + <string>#747474</string> + </dict> + </dict> + <dict> + <key>name</key> + <string>Illegal</string> + <key>scope</key> + <string>invalid.illegal</string> + <key>settings</key> + <dict> + <key>background</key> + <string>#7c7c7c</string> + <key>foreground</key> + <string>#101010</string> + </dict> + </dict> + <dict> + <key>name</key> + <string>Broken</string> + <key>scope</key> + <string>invalid.broken</string> + <key>settings</key> + <dict> + <key>background</key> + <string>#999999</string> + <key>foreground</key> + <string>#f7f7f7</string> + </dict> + </dict> + <dict> + <key>name</key> + <string>Deprecated</string> + <key>scope</key> + <string>invalid.deprecated</string> + <key>settings</key> + <dict> + <key>background</key> + <string>#5e5e5e</string> + <key>foreground</key> + <string>#101010</string> + </dict> + </dict> + <dict> + <key>name</key> + <string>Unimplemented</string> + <key>scope</key> + <string>invalid.unimplemented</string> + <key>settings</key> + <dict> + <key>background</key> + <string>#ababab</string> + <key>foreground</key> + <string>#101010</string> + </dict> + </dict> + </array> + <key>uuid</key> + <string>uuid</string> +</dict> +</plist> |