def anagram_2d_array(self): """ This method is used to store prime anagram and prime number which are not anagram in matrix or 2d array. and print them accordingly """ prime_list = Utility.get_prime() # get prime numbers anagram_list = Utility.get_anagram_prime( ) # get anagram numbers which is prime also not_anagram = [] row = 10 column = 25 two_d_array = [[0 for j in range(column)] for i in range(row)] k = 0 index = 0 for i in prime_list: if anagram_list.__contains__( i) is not True: # if file not contains same element not_anagram.append(i) # append into not_anagram list for i in range(row): for j in range(column): if k < len(anagram_list): two_d_array[i][j] = anagram_list[ k] # add element of anagram list into array k += 1 if k >= len(anagram_list) and index < len(not_anagram): two_d_array[i][j] = not_anagram[ index] # add element of not_anagram list k += 1 # into same array after anagram_list index += 1 for i in range(row): for j in range(column): if two_d_array[i][j] != 0: print(two_d_array[i][j], end=" ") # print 2d array print()
def anagram_stack(self): """ This method is used to print prime anagram in reverse order. """ for i in Utility.get_anagram_prime( ): # get anagram numbers which are prime stack.push(i) # push numbers into stack for i in range(0, stack.size()): print(stack.pop()) # print in reverse order
def anagram_queue(self): """ This method is used to print prime anagram using queue. """ for i in Utility.get_anagram_prime( ): # get anagram numbers which are prime queue1.enqueue(i) # add numbers into queue for i in range(0, queue1.size()): print(queue1.dequeue()) # print in reverse order
def prime_number_2d_array(self): """ This method is used to store prime number in matrix or 2d array and print in proper order. """ prime_list = Utility.get_prime() # get prime number row = 10 column = 25 limit = 100 # create empty 2d array two_d_array = [[0 for loop in range(column)] for loop in range(row)] loop3 = 0 for loop1 in range(row): for loop2 in range(column): if loop3 < len(prime_list): if prime_list[ loop3] <= limit: # prime number check with the limit two_d_array[loop1][loop2] = prime_list[ loop3] # add number into array loop3 += 1 limit += 100 # increment limit by 100 for each iteration for loop4 in range(row): for loop5 in range(column): if two_d_array[loop4][loop5] != 0: print(two_d_array[loop4][loop5], end=" ") # display elements in 2d array format print()
"""Take a range of 0 - 1000 Numbers and find the Prime numbers in that range.""" from UtilityMethods import Utility Utility.prime()
""" To the Util Class add temperature Conversion static function, given the temperature in fahrenheit as input outputs the temperature in Celsius or viceversa using the formula Celsius to Fahrenheit: (°C × 9/5) + 32 = °F Fahrenheit to Celsius: (°F − 32) x 5/9 = °C""" from UtilityMethods import Utility cel = float(input("enter the Celsius value \n")) Utility.temp(cel)
""" I/P -> The number of times to Flip Coin. Ensure it is positive integer. Logic -> Use Random Function to get value between 0 and 1. If < 0.5 then tails or heads O/P -> Percentage of Head vs Tails""" from UtilityMethods import Utility try: number = int(input("enter the number of time the coin to be flipped \n")) Utility.flip_coin(number) # calling the flip coin method except ValueError: print("enter the integer value")
""" Desc -> There is 1, 2, 5, 10, 50, 100, 500 and 1000 Rs Notes which can be returned by Vending Machine. Write a Program to calculate the minimum number of Notes as well as the Notes to be returned by the Vending Machine as a Change I/P -> read the Change in Rs to be returned by the Vending Machine Logic -> Use Recursion and check for largest value of the Note to return change to get to minimum number of Notes. O/P -> Two Outputs - one the number of minimum Note needed to give the change and second list of Rs Notes that would given in the Change """ from UtilityMethods import Utility try: rupee = int(input("enter the rupees\n")) Utility.vending_machine(rupee) except Exception as e: print(e)
""" Desc -> Computes the prime factorization of N using brute force. I/P -> Number to find the prime factors Logic -> Traverse till i*i <= N instead of i <= N for efficiency. O/P -> Print the prime factors of number N.""" from UtilityMethods import Utility try: number = int(input("enter the number \n")) Utility.prime_number(number) except NameError as e: print(e)
""" Extend the above program to find the prime numbers that are Anagram and Palindrome """ from UtilityMethods import Utility number = int(input("enter the number \n")) Utility.palindrome(number)
""" Desc -> Write a Stopwatch Program for measuring the time that elapses between the start and end clicks I/P -> Start the Stopwatch and End the Stopwatch Logic -> Measure the elapsed time between start and end O/P -> Print the elapsed time.""" from UtilityMethods import Utility try: a = int(input("Press 0 to start the time ")) b = int(input("Press 1 to Stop the time ")) Utility.start1() Utility.stop1() Utility.elapsed_time() except ValueError: print("Invalid Input")
def calender(self, month, year): """ This method is used to print Calender of given month and year. month : month given by user year : year given by user """ day = ['S', ' M', ' T', ' W', ' Th', 'F', ' S'] days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] values = 1 d = 1 m = month y = year y0 = y - (14 - m) // 12 x = y0 + y0 // 4 - y0 // 100 + y0 // 400 m0 = m + 12 * ((14 - m) // 12) - 2 d0 = (d + x + 31 * m0 // 12) % 7 print(d0) if Utility.leap_year(year): # check leap year days[1] = 29 row = 6 column = 7 two_d_array = [[0 for j in range(column)] for i in range(row)] # create empty 2d array print('Your Calender is\n') for i in range(0, 6 + 1): print(day[i], end=' ') # print day's for calender print() for i in range(row): for j in range(column): if values <= days[m - 1]: if i == 0 and j < d0: # while d0 is less than j two_d_array[i][j] = ' ' # it will print blank space continue two_d_array[i][j] = values # add days into calender values += 1 # increment counter for i in range(row): for j in range(column): if two_d_array[i][j] != 0: x = two_d_array[i][j] # l just() method returns the string x1 = str(x).ljust( 2) # left justified in a string of length width. print(x1, end=" ") print()
""" Desc -> Reads in integers prints them in sorted order using Bubble Sort I/P -> read in the list ints O/P -> Print the Sorted List """ from UtilityMethods import Utility try: # calling bubble sort method Utility.integer_bubble_sort() except Exception as e: print(e)
""" Desc -> Given N distinct Coupon Numbers, how many random numbers do you need to generate distinct coupon number? This program simulates this random process. I/P -> N Distinct Coupon Number Logic -> repeatedly choose a random number and check whether it's a new one. O/P -> total random number needed to have all distinct numbers. Functions => Write Class Static Functions to generate random number and to process distinct coupons. """ from UtilityMethods import Utility try: coupon_number = int(input("Enter the Coupon number\n")) Utility.coupon(coupon_number) except ValueError as v: print(v) print("enter integer data ")
""" Desc -> A library for reading in 2D arrays of integers, doubles, or booleans from standard input and printing them out to standard output. I/P -> M rows, N Cols, and M * N inputs for 2D Array. Use Java Scanner Class Logic -> create 2 dimensional array in memory to read in M rows and N cols O/P -> Print function to print 2 Dimensional Array. In Java use PrintWriter with OutputStreamWriter to print the output to the screen.""" from UtilityMethods import Utility row = int(input("enter the row size ")) col = int(input("enter the col size ")) list1 = [[0 for i in range(col)] for j in range(row)] Utility.tow_d_array(row, col, list1)
def calender_queue(self, month, year): """ This method is used to print calender of given month and year. In this method calender is created using queue month : month given ser year : year given by year """ day = ['S', ' M', ' T', ' W', ' Th', 'F', ' S'] days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] values = 1 d = 1 m = month y = year y0 = y - (14 - m) // 12 x = y0 + y0 // 4 - y0 // 100 + y0 // 400 m0 = m + 12 * ((14 - m) // 12) - 2 d0 = (d + x + 31 * m0 // 12) % 7 if Utility.leap_year(year): # check leap year days[1] = 29 row = 6 column = 7 print('Your Calender is\n') for i in range(0, 6 + 1): print(day[i], end=' ') # print day's for calender print() for i in range(row): for j in range(column): if values <= days[m - 1]: # while d0 is less than j if i == 0 and j < d0: # it will print blank space queue1.enqueue( ' ') # used enqueue() method of queue class continue # to add space queue1.enqueue(values) # add element in queue values += 1 for i in range(row): for j in range(column): if queue1.size() > 0: x = queue1.dequeue( ) # remove element from queue and store it in x variable x1 = str(x).ljust( 2) # using l just() method print formatted calender print(x1, end=" ") print()
""" User Input and Replace String Template “Hello <<UserName>>, How are you?” I/P -> Take User Name as Input. Ensure UserName has min 3 char Logic -> Replace <<UserName>> with the proper name O/P -> Print the String with User Name""" from UtilityMethods import Utility statement = "hello username, how are you?" # username should have minimum 3 characters replaced_name = input("enter the name to be replaced \n") Utility.replace(statement, replaced_name)
def calender_stack(self, month, year): """ This method is used to print calender of given month and year. In this method calender is created using stack month : month given by user year : year given by user """ day = ['S', ' M', ' T', ' W', ' Th', 'F', ' S'] days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] values = 1 d = 1 m = month y = year y0 = y - (14 - m) // 12 x = y0 + y0 // 4 - y0 // 100 + y0 // 400 m0 = m + 12 * ((14 - m) // 12) - 2 d0 = (d + x + 31 * m0 // 12) % 7 if Utility.leap_year(year): # check leap year days[1] = 29 row = 6 column = 7 print('Your Calender is Ready\n') for i in range(0, 6 + 1): print(day[i], end=' ') # print day's for calender print() for i in range(row): for j in range(column): if values <= days[m - 1]: # while d0 is less than j if i == 0 and j < d0: # it will print blank space stack.push(' ') # use push() to add blanks continue stack.push(values) # add days using push() method values += 1 for i in range(stack.size()): stack_element = stack.pop( ) # pop element from stack and store in local variable stack1.push( stack_element) # again push element into stack for reverse for i in range(row): for j in range(column): if stack1.size() > 0: x = stack1.pop( ) # access element one by one using pop() method x1 = str(x).ljust( 2) # l just() method to print in calender structure print(x1, end=" ") print()
""" I/P -> Year, ensure it is a 4 digit number. Logic -> Determine if it is a Leap Year. O/P -> Print the year is a Leap Year or not.""" from UtilityMethods import Utility try: year = int(input("enter the year \n")) Utility.leap_year(year) except Exception as e: print(e) print("enter the integer")
""" Write a program WindChill.java that takes two double command-line arguments t and v and prints the wind chill. Use Math.pow(a, b) to compute ab. Given the temperature t (in Fahrenheit) and the wind speed v (in miles per hour), the National Weather Service defines the effective temperature (the wind chill) to be: w=35.74 + 0.0215t + (0.4275t - 35.75)* v^0.16 Note: the formula is not valid if t is larger than 50 in absolute value or if v is larger than 120 or less than 3 (you may assume that the values you get are in that range).""" from UtilityMethods import Utility try: temperature = float(input("enter the temperature \n")) wind_speed = float(input("enter the speed of the wind \n")) Utility.windchill(temperature, wind_speed) except Exception as e: print(e) print("do not enter character value")
""" Desc -> takes a command-line argument N, asks you to think of a number between 0 and N-1, where N = 2^n, and always guesses the answer with n questions. I/P -> the Number N and then recursively ask true/false if the number is between a high and low value Logic -> Use Binary Search to find the number O/P -> Print the intermediary number and the final answer """ from UtilityMethods import Utility import math try: number = int(input("How much time you want me to ask the question:")) low = 0 high = int(math.pow(2, number)) print("Think a number between(", low, ")to(", high,")in range") result = Utility.search_number(low, high) print("The number is = ", result) except Exception as e: print(e)
""" Desc -> Read in a list of words from File. Then prompt the user to enter a word to search the list. The program reports if the search word is found in the list. I/P -> read in the list words comma separated from a File and then enter the word to be searched Logic -> Use Arrays to sort the word list and then do the binary search O/P -> Print the result if the word is found or not""" from UtilityMethods import Utility try: Utility.string_binary() except Exception as e: print(e)
""" Desc -> Simulates a gambler who start with $stake and place fair $1 bets until he/she goes broke (i.e. has no money) or reach $goal. Keeps track of the number of times he/she wins and the number of bets he/she makes. Run the experiment N times, averages the results, and prints them out. I/P -> $Stake, $Goal and Number of times Logic -> Play till the gambler is broke or has won O/P -> Print Number of Wins and Percentage of Win and Loss.""" from UtilityMethods import Utility try: stake = int(input("enter the stake value \n")) goals = int(input("enter the goals \n")) trails = int(input("enter the number of trails \n")) Utility.gambler_method(stake, goals, trails) except Exception as e: print(e) print("enter integer value ")
""" Desc -> Reads in strings from standard input and prints them in sorted order. Uses insertion sort. I/P -> read in the list words Logic -> Use Insertion Sort to sort the words in the String array O/P -> Print the Sorted List """ from UtilityMethods import Utility try: Utility.string_insertion_sort() except Exception as e: print(e)
"""" Desc -> Prints the Nth harmonic number: 1/1 + 1/2 + ... + 1/N (http://users.encs.concordia.ca/~chvatal/notes/harmonic.html). I/P -> The Harmonic Value N. Ensure N != 0 Logic -> compute 1/1 + 1/2 + 1/3 + ... + 1/N O/P -> Print the Nth Harmonic Value.""" from UtilityMethods import Utility number = int(input("enter the number \n")) Utility.harmonic(number)
""" Write a program Quadratic.java to find the roots of the equation a*x*x + b*x + c. Since the equation is x*x, hence there are 2 roots. The 2 roots of the equation can be found using a formula delta = b*b - 4*a*c Root 1 of x = (-b + sqrt(delta))/(2*a) Root 2 of x = (-b - sqrt(delta))/(2*a) Take a, b and c as input values to find the roots of x.""" from UtilityMethods import Utility try: a = float(input("enter a value \n")) b = float(input("enter b value \n")) c = float(input("enter c value \n")) Utility.quadratic(a, b, c) except Exception as e: print(e) print("enter the valid number ")
""" Desc -> A program with cubic running time. Read in N integers and counts the number of triples that sum to exactly 0. I/P -> N number of integer, and N integer input array Logic -> Find distinct triples (i, j, k) such that a[i] + a[j] + a[k] = 0 O/P -> One Output is number of distinct triplets as well as the second output is to print the distinct triplets.""" from UtilityMethods import Utility try: size = int(input("enter the size of list\n")) list1 = [] print("enter the values ") for i in range(0, size): val = int(input()) list1.append(val) Utility.triplets(size, list1) except Exception as e: print(e)
""" Write a static function toBinary that outputs the binary (base 2) representation of the decimal number typed as the input. It is based on decomposing the number into a sum of powers of 2. For example, the binary representation of 106 is 11010102, which is the same as saying that 106 = 64 + 32 + 8 + 2. Ensure necessary padding to represent 4 Byte String. To compute the binary representation of n, we consider the powers of 2 less than or equal to n in decreasing order to determine which belong in the binary decomposition (and therefore correspond to a 1 bit in the binary representation). """ from UtilityMethods import Utility try: number = int(input("enter the number\n")) Utility.to_binary(number) except Exception as e: print(e)
binarySearch method for integer binarySearch method for String insertionSort method for integer insertionSort method for String bubbleSort method for integer bubbleSort method for String I/P -> Write main function to call the utility function Logic -> Check using Stopwatch the Elapsed Time for every method call O/P -> Output the Search and Sorted List. More importantly print elapsed time performance in descending order """ from UtilityMethods import Utility number = int(input("enter the number to perform operations\n")) if number == 1: Utility.string_insertion_sort() elif number == 2: Utility.integer_insertion_sort() elif number == 3: Utility.string_binary() elif number == 4: Utility.integer_binary() elif number == 5: Utility.integer_bubble_sort() elif number == 6: Utility.string_bubble_sort() else: print("invalid data ")
""" Desc -> This program takes a command-line argument N and prints a table of the powers of 2 that are less than or equal to 2^N. I/P -> The Power Value N. Only works if 0 <= N < 31 since 2^31 overflows an int Logic -> repeat until i equals N.""" from UtilityMethods import Utility try: number = int(input("enter the number\n")) Utility.power_of_two(number) except Exception as e: print(e) print("enter integer value")