<jupyter_start><jupyter_text>**2022 Python Programming Practical 3** **Q0. (Simple calculator)**  Write a program to simulate a simple calculator with the four basic operations of addition, subtraction, multiplication and division. Use functions.<jupyter_code>def add (n1, n2):
    return n1 + n2

def subtract (n1, n2):
    return n1 - n2

def multiply (n1, n2):
    return n1 * n2

def divide (n1, n2):
    return n1 / n2

eqn = input ("Enter equation to compute: ")
print(add (3,2))
print(subtract (5,4))
print(multiply (5, 6))
print(divide (6, 5))<jupyter_output>5
1
30
1.2<jupyter_text>**Q1. (Displaying an integer reversed)**Write a function reverse_int(n) to display an integer in reverse order:For example, reverse_int(3456) displays 6543.<jupyter_code>num = int(input('Enter number to be reversed: '))

def reverse_int(n):
    rev = 0                         #initialise value
    while n>0:                      
        rev = rev * 10 + n % 10     #getting the last value of num and adding as last digit of rev
        n = n // 10                 #remove last value of num
    return rev

print (reverse_int(num))<jupyter_output>Enter number to be reversed: 3456
6543<jupyter_text>**Q2. (Displaying patterns)**Write a function display_pattern(n) to display a pattern as follows:```              1            2 1          3 2 1...n n-1 ... 3 2 1 ```<jupyter_code>def display_pattern(n):
    for i in range (1, n + 1):
        width = 2* n + 1                #setting width of column
        num = '1'
        for j in range (1,i):           #loop to add to number
            num = str(j+1) + ' ' + num 
        print (f'{num:>{width}}')

display_pattern (9)<jupyter_output>1
                2 1
              3 2 1
            4 3 2 1
          5 4 3 2 1
        6 5 4 3 2 1
      7 6 5 4 3 2 1
    8 7 6 5 4 3 2 1
  9 8 7 6 5 4 3 2 1<jupyter_text>How about the following pattern?```    1   121  12321 1234321123454321```<jupyter_code>def display_pattern(n):
    for i in range(1,n+1):
        width = 2* n -1
        num = '1'
        for j in range (1,i):
            num = str(j+1) + num + str(j+1) 
        print (num.center(width))

display_pattern (9)<jupyter_output>1        
       212       
      32123      
     4321234     
    543212345    
   65432123456   
  7654321234567  
 876543212345678 
98765432123456789<jupyter_text>**Q3. (Computing GCD)**Write a function gcd(m, n) that returns the greatest common divisor between two positive integers:Test your program with gcd(24, 16) and gcd(255, 25).<jupyter_code>def gcd (m, n):
    lcm = max(m, n)                           #using lcm method. initialise search with the larger value of m and n
    while (lcm % m != 0) or (lcm % n !=0):    #checking for lcm
        lcm += 1                              #increment value of lcm if not divisible by m or n
    return int ((m * n)/lcm)                  #return gcd, using lcm formula

print (gcd(24,16))

print (gcd(255,25))

def gcd (m, n):
    value = min(m, n)                           #finding the smaller value of n1 and n2
    while m % value != 0 or n % value != 0:     #testing if current value of gcd is not a factor of either n1 and n2
        value -= 1                              #decrement for next while loop
    return value
  
print (gcd(24,16))

print (gcd(255,25))<jupyter_output>8
5<jupyter_text>**Q4. (Summing series)**Write a function m_series(i) to compute the following series:m(i) = ½ + ⅔ + … + i/(i+1)where i >= 1. Generate the following table:```i         m(i)     1         0.5000   2         1.1667   ... 19        16.4023  20        17.3546  ```<jupyter_code>def m_series(i):
    print('{:10}{}'.format('i','m(i)'))
    for num in range(1, i+1):                       #for loop to set value of i in each row
        m_i = 0
        for j in range (1, num+1):                  #for loop for summation of m_i
            m_i += j / (j+1)
        print('{:<10}{:.4f}'.format(num, m_i))      #printing each row

m_series(10)<jupyter_output>i         m(i)
1         0.5000
2         1.1667
3         1.9167
4         2.7167
5         3.5500
6         4.4071
7         5.2821
8         6.1710
9         7.0710
10        7.9801<jupyter_text>**Q5. (Prime number)**Write a function to determine whether an integer is a prime number. An integer greater than 1 is a prime number if its only divisors are 1 and itself. For example, is_prime(11) returns True, and is_prime(9) returns False.Use the is_prime(n) function to find the first thousand prime numbers and display every ten prime numbers in a row, as follows:```2   3   5   7   11   13   17   19   23   2931  37  41  43  47   53   59   61   67   7173  79  83  89  97  ......```<jupyter_code>def is_prime(n):
    i = 2                   #smallest prime number
    while i < n:            
        if n % i == 0:      #checking if divisible
            return False    #returns False if not prime number
        i += 1              #increment i to continue while loop
    return True             #returns True if it can't be divided by all numbers between 1 and itself

#main
num = 2                                             #smallest prime number
for row in range (100):                             #printing rows
    for col in range (10):                          #printing columns
        while is_prime(num) == False:               #call is_prime function to check if num is prime. While loops continue to loop if not prime
            num += 1                                #increment num if not prime
        print ('{:<4}'.format(num), end = ' ')      #print prime number
        num += 1                                    #increment num for next search
    print()                                         #/n for end of each row<jupyter_output>2    3    5    7    11   13   17   19   23   29   
31   37   41   43   47   53   59   61   67   71   
73   79   83   89   97   101  103  107  109  113  
127  131  137  139  149  151  157  163  167  173  
179  181  191  193  197  199  211  223  227  229  
233  239  241  251  257  263  269  271  277  281  
283  293  307  311  313  317  331  337  347  349  
353  359  367  373  379  383  389  397  401  409  
419  421  431  433  439  443  449  457  461  463  
467  479  487  491  499  503  509  521  523  541  
547  557  563  569  571  577  587  593  599  601  
607  613  617  619  631  641  643  647  653  659  
661  673  677  683  691  701  709  719  727  733  
739  743  751  757  761  769  773  787  797  809  
811  821  823  827  829  839  853  857  859  863  
877  881  883  887  907  911  919  929  937  941  
947  953  967  971  977  983  991  997  1009 1013 
1019 1021 1031 1033 1039 1049 1051 1061 1063 1069 
1087 1091 1093 1097 1103 1109 1117 1123 1129 1151 
1153 1163 1171 1181 1187 1193 1[...]<jupyter_text>**Q6. (Printing matrices)**Write a function print_matrix(n) that displays an n by n matrix, where n is a positive integer entered by the user. Each element is 0 or 1, which is generated randomly. A 3 by 3 matrix may look like this:```0 1 00 0 01 1 1 ```<jupyter_code>#importing random module
import random

def print_matrix(n):
    for row in range (n):
        for col in range (n):
            print (random.randint(0,1), end = ' ')
        print()

#main
size = int(input('Enter size of matrix: '))
print_matrix(size)<jupyter_output>Enter size of matrix: 5
0 0 1 0 0 
1 1 1 1 1 
0 0 1 0 0 
1 1 0 1 0 
0 1 1 1 1<jupyter_text>**Q7. (Converting milliseconds to hours, minutes, and seconds)**Write a method convert_ms(n) that converts milliseconds to hours, minutes, and seconds. The method returns a string as hours:minutes:seconds. For example, convert_ms(5500) returns a string 0:0:5, convert_ms(100000) returns a string 0:1:40, and convert_ms(555550000) returns a string 154:19:10.<jupyter_code>def convert_ms(n):
    sec = (n//1000) % 60                                #converts ms to sec, and then modulus to get a remainder between 1 and 60
    min_calc = n // (60 * 1000)                         #converts ms to min, for further calculation
    min = min_calc % 60                                 #gets remainder of minutes, betwee 1 and 60
    hrs = min_calc // 60                                #calculate number of hours
    return str(hrs) + ':' + str(min) + ':' + str(sec)
  
#print(convert_ms(555550000))
#print(convert_ms(100000))

#main
time = int(input('Enter time in milliseconds: '))
print('Converting to h:m:s')
print(convert_ms(time))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.08605200945626477, 'fraction_numerical': 0.13605200945626478, 'mean_word_length': 1.9175862068965517, 'pattern_counts': {'":': 0, '<': 32, '<?xml version=': 0, '>': 32, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 37, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '260745', 'license': 'MIT', 'n_tokens_mistral': 3524, 'n_tokens_neox': 2781, 'n_words': 1111, 'path': 'JJ_Cheung_2022_Python_Programming_Practical_3.ipynb', 'repo_name': 'jjcheung/ctcuc22', 'size': '8460'}