Posts

Showing posts from September, 2024

Programming Assignment - 10

Programming Assignment - Week 10 Note: There could be optimised ways to solve the problem, but the goal here is to solve it effectively. The most intuitive solution has been provided for clarity. Programming Assignment 1 Question: Write a program that accepts the string para and a positive integer n as input. The program should print 1 if there is at least one word in para that occurs exactly n times, and 0 otherwise. Input Format: The input consists of two lines. The first line contains the string para , which is a sequence of space-separated words. The second line contains a positive integer n . Output Format: A single integer, either 1 or 0 . Print 1 if there is at least one word that occurs exactly n times. Print 0 otherwise. Example: Input: apple orange apple banana orange banana banana 2 Output: 1 Code: from collections impor...

Week - 10 Quiz Assignment

Quiz Week 10 : Assignment 10 1. What will be the output of the following Python code? s = "Hello, World!" print (s[7:12]) "World" "World!" "Worl" "orld" 2. Which string method would you use to remove all leading and trailing whitespace from a string in Python? strip() split() replace() join() 3. Given the string s = "PythonProgramming", what does s [::2] return? "PythonProgramming" "Pto rgamn" "PyonPormig" "PtoPormig" 4. Why are names often converted to lowercase and spaces removed when implementing the FLAMES game in Python? To increase game difficulty To ensure consistent character comparison Because uppercase letters are not supported in Python strings ...

Programming Assignment - 9

Programming Assignment - Week 9 Note: There could be optimised ways to solve the problem, but the goal here is to solve it effectively. The most intuitive solution has been provided for clarity. Programming Assignment 1 Question: Write a Python function that counts the number of vowels in a given string s . The function should return the number of vowels found, considering both uppercase and lowercase vowels. Input Format: The input consists of a single string s . Output Format: A single integer representing the count of vowels in the string. Example: Input: hello world Output: 3 Code: def count_vowels ( s ): vowels = { 'a' , 'e' , 'i' , 'o' , 'u' , 'A' , 'E' , 'I' , 'O' , 'U' } vowel_count = 0 for char in s : if char in vowels : vowel_count += 1 r...

The joy of computing using Python Week - 9

Quiz Week 9 : Assignment 9 1. ‘nltk.download()‘ function downloads necessary packages for the Natural Language Toolkit (NLTK) library? True False 2. Which of the following best defines a complete graph? A graph where every pair of distinct vertices is connected by a unique edge A graph with no edges A graph with a single vertex A graph with at least one loop 3. How many edges are there in a complete graph with 4 nodes? 6 8 12 16 4. Which Python library is most commonly used for working with graphs related to networks? Random Pandas NumPy NetworkX 5. Gephi is: A Python library for linear algebra A software for visualizing and analyzing large networks A tool for data cleaning and preprocessing A Python library for building statistical models ...

Programming Assignment - 8

Programming Assignment - Week 8 Note: There could be optimised ways to solve the problem, but the goal here is to solve it effectively. The most intuitive solution has been provided for clarity. Programming Assignment 1 Question: Write a Python function that takes an integer n followed by n lines of space-separated integers, where each line represents a tuple. The function should return the sum of the first elements of all tuples. Input Format: The first line contains a single integer n , which represents the number of tuples. The next n lines each contain two space-separated integers representing the values of the tuple. Output Format: A single integer representing the sum of the first elements of all tuples. Example: Input: 3 3 4 1 2 5 6 Output: 9 Code: def sum_of_first_elements (): # Read the number of tuples n = int ( input ()) # Initialize the sum ...

The joy of computing using Python Week - 8

Quiz Week 8: Assignment 8 1. Which of the following is a valid way to create a tuple in Python? t = [1, 2, 3] t = (1, 2, 3) t = {1, 2, 3} t = 1, 2, 3 2. Which of the following operations is valid on a tuple? t[1] = 4 t.append(4) t = t + (4,) del t[1] 3. What will the following code output? t = [1, 2, 3] print(type((t, t))) <class 'list'> <class 'set'> <class 'tuple'> Error 4. What is the primary purpose of the matplotlib.pyplot module in Python? To perform matrix operations. To handle file I/O operations. To generate and customize visualizations like plots and graphs. To manipulate and process images. 5. Which of the following statements is true about anagrams? Two strings of different lengths can be anagra...

Programming Assignment - 7

Programming Assignment - Week 7 Note: There could be optimised ways to solve the problem, but the goal here is to solve it effectively. The most intuitive solution has been provided for clarity. Week 7: Programming Assignment 1 Question: You are given a board game scenario with ladders and snakes. A player starts at a given position on the board, and you are provided with the result of a die roll. You need to determine whether the player lands on a ladder, a snake, or a normal block after moving. Write a Python function named move_player that takes four inputs: ladders : A list containing the indices of blocks with ladders. snakes : A list containing the indices of blocks with snakes. current_position : An integer representing the player's current position on the board. die_roll : An integer representing the result of a die roll. The function should return...

The Joy of Computing using Python Week-7

Quiz Week 7: Assignment 7 1. Which of the following methods is used to read the content of a CSV file in Python using the csv module? csv.reader() csv.write() csv.load() csv.readfile() 2. Which command is used to install a Python package using pip? pip install package-name install pip package-name python install package-name pip package-name install 3. What is the primary purpose of the gmplot library in Python? To create 3D plots To plot data on Google Maps To generate matplotlib graphs To create dashboards 4. In a game of Snakes and Ladders, a player is currently on square 96. There is a snake on square 99 that sends the player back to square 78. If the player wishes to reach square 100 in one dice t...

Programming assignment - Week 6

Programming assignment - Week 6  Solutions Note: There could be optimised ways to solve the problem, but the goal here is to solve it effectively. The most intuitive solution has been provided for clarity.

The Joy of Computing using Python Week-6

Quiz Week 6: Assignment 6 Solution

week-5 Question-4 Discrepancy

Week-5 Question-4 Discrepancy Student's Logic: Hi, I wanted to bring up an issue with the number of iterations needed in binary search. In the problem, it's mentioned that binary search takes 10 iterations for a list of size 1024, but it should actually be 11 iterations. If we start with a list of size 1024, the binary search will first check the middle element, splitting the list in half repeatedly. The number of iterations in binary search for a list of size n is given by log2(n) + 1 . So, for 1024 elements: log2(1024) = 10 , which means there are 10 splits. However, we also need to account for the initial check before the first split, making the total number of iterations 11. Let's take it for a smaller list of 2 elements, say {1, 2} and target=2, first s=0, e=1 . This gives mid=0 , this would be the first comparison since arr[mid] < target so s=mid+1 , so s=1 ...