Assignment 1

pdf

School

Northeastern University *

*We aren’t endorsed by this school

Course

6400

Subject

Computer Science

Date

Apr 3, 2024

Type

pdf

Pages

4

Uploaded by ColonelMosquitoMaster1017

Report
Question 1: Write a Python program that repeatedly takes input from the user for a numerical value. The program should prompt the user to enter a positive number. If the user enters a negative number or any non-numeric input, display an error message and keep prompting the user to enter a positive number until they provide one. Once a positive number is obtained, convert it to a string, concatenate it with a predefined string of your choice, and print out the result. Your program should perform the following steps: 1. Continuously prompt the user to enter a numerical value. 2. Check if the input is a positive number. 3. If the input is a positive number, convert it to a string. 4. Concatenate the converted string with a predefined string. 5. Print the result. In [1]: Question 2: You are given a list of dictionaries containing data as follows: data = [ {"name":"Alice","age":25,"city":"New York","hobbies":["Reading","Painting"]}, {"name":"Bob","age":30,"city":"Los Angeles","hobbies":["Cooking","Hiking"]}, {"name":"Charlie","age":22,"city":"New York","hobbies":["Hiking","Gardening"]}, {"name":"David","age":35,"city":"Chicago","hobbies":["Reading","Swimming"]} ] Each dictionary represents information about a person: their name, age, city, and hobbies. Your task is to write a Python function filter_persons(data, min_age, target_city, required_hobbies) that takes this list of dictionaries and three arguments - min_age (an integer), target_city (a string), and required_hobbies (a list of strings) as parameters. The function should return a list of names of people who meet the following criteria: 1. They are at least min_age years old. Enter a positive number-10 Not a positive number. Please try again! Enter a positive number0 Not a positive number. Please try again! Enter a positive number1 Result: Hi, The positive number you have entered is 1 i = 1 predefined_str = "Hi, The positive number you have entered is " while i > 0 : number = input ( "Enter a positive number" ) if number.isnumeric(): if int (number) > 0 : new_str = predefined_str + str (number) print ( "Result: " ,new_str) break print ( "Not a positive number. Please try again!" ) Assignment 1 - Jupyter Notebook http://localhost:8888/notebooks/Documents/Data%20Analytics%20Eng... 1 of 4 9/22/23, 7:44 PM
2. They live in the specified target_city. 3. They have at least one of the hobbies listed in required_hobbies." In [2]: Question 3: Given the string data_analytics = "Data analytics uncovers insights from data to guide decisions.", perform string slicing operations to generate the following four outputs: 1. "Data data" 2. "Guide to decisions" 3. "Uncovers insights" 4. "noisiced ediug" You should use slicing techniques to extract and manipulate the required substrings from the given data_analytics string to achieve these specific outputs. ['Alice', 'Charlie'] data = [ { "name" : "Alice" , "age" : 25 , "city" : "New York" , "hobbies" :[ "Reading" , "P { "name" : "Bob" , "age" : 30 , "city" : "Los Angeles" , "hobbies" :[ "Cooking" , " { "name" : "Charlie" , "age" : 22 , "city" : "New York" , "hobbies" :[ "Hiking" , " { "name" : "David" , "age" : 35 , "city" : "Chicago" , "hobbies" :[ "Reading" , "Sw ] def filter_persons (data,min_age,target_city,required_hobbies): people = [] for i in range ( len (data)): if (data[i][ "age" ] >= min_age) and (data[i][ "city" ].upper() == tar for j in required_hobbies: if j in data[i][ "hobbies" ]: people.append(data[i][ "name" ]) break return people print (filter_persons(data, 22 , "New York" ,[ "Hiking" , "Painting" ])) Assignment 1 - Jupyter Notebook http://localhost:8888/notebooks/Documents/Data%20Analytics%20Eng... 2 of 4 9/22/23, 7:44 PM
In [3]: Question 4: Create three variables and assign each a string value. Then, combine the variables in a way that results in a single string with all three words separated by commas. In [4]: Question 5: Create a Python program that helps users handle escape characters in their input strings. The program should enable users to input strings that contain escape sequences such as "\n" for a newline or "\t" for a tab. It should then ensure that these escape sequences are interpreted correctly, resulting in the desired output being displayed. String 1: Data data String 2: Guide to decision String 3: Uncovers insights String 4: noisiced ediug The result string: Data,Analytics,Engineering data_analytics = "Data analytics uncovers insights from data to guide #Data data str_1_first = data_analytics[: 5 ] str_1_last = data_analytics[ - 24 : - 20 ] str_1 = str_1_first + str_1_last print ( "String 1: " , str_1) #Guide to decisions str_2_working = data_analytics[ - 19 : - 2 ] str_2_first = str_2_working[ 3 ].upper() + str_2_working[ 4 : 9 ] str_2_middle = str_2_working[: 3 ] str_2_last = str_2_working[ - 8 :] str_2 = str_2_first + str_2_middle + str_2_last print ( "String 2: " , str_2) #Uncovers insights str_3 = data_analytics[ 15 ].upper() + data_analytics[ 16 : 32 ] print ( "String 3: " , str_3) #noisiced ediug str_4 = data_analytics[ - 16 : - 2 ] list_str_4 = list (str_4) list_str_4.reverse() str_4 = '' .join(list_str_4) print ( "String 4: " , str_4) str_1 = "Data" str_2 = "Analytics" str_3 = "Engineering" result_str = "," .join([str_1,str_2,str_3]) print ( "The result string: " ,result_str) Assignment 1 - Jupyter Notebook http://localhost:8888/notebooks/Documents/Data%20Analytics%20Eng... 3 of 4 9/22/23, 7:44 PM
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
In [5]: Question 6: Given an unsorted list of numbers, write a program to find the median numbers in the list. Use the following list as an example: numbers = [5, 1, 3, 2, 4]. In [6]: Enter str with escape characters:a\nb a b Enter the length of list: 5 Enter the list of numbers : 5 1 3 2 4 The Median is: 3 x = input ( "Enter str with escape characters:" ) output_str = x.encode().decode( 'unicode_escape' ) print (output_str) list_len = int ( input ( "Enter the length of list: " )) list_num = list ( map ( int , input ( "\nEnter the list of numbers : " ).strip list_num.sort() median_index = (list_len // 2 ) if list_len % 2 != 0 : median = list_num[median_index] else : median = (list_num[median_index] + list_num[median_index - 1 ]) / 2 print ( 'The Median is: ' , median) Assignment 1 - Jupyter Notebook http://localhost:8888/notebooks/Documents/Data%20Analytics%20Eng... 4 of 4 9/22/23, 7:44 PM