HW2- Fall 2023 (1).ipynb - Colaboratory

pdf

School

University of California, Berkeley *

*We aren’t endorsed by this school

Course

MISC

Subject

Computer Science

Date

Dec 6, 2023

Type

pdf

Pages

3

Uploaded by DeaconBat3708

Report
9/14/23, 6:40 PM HW2- Fall 2023 (1).ipynb - Colaboratory https://colab.research.google.com/drive/16ADDoXrW3gW9qG83sEjo-4zHTcZahi1y#scrollTo=XLwbCtF2OCj9&printMode=true 1/3 String Methods + List the string methods are used to modify and manipulate the string in various ways, such as converting the string to lowercase or uppercase, splitting the string into a list of words, replacing a substring, ¦nding the index of a substring, checking if the string starts or ends with a certain substring, removing whitespace from the start and end of the string, ¦nding the length of the string, and joining a list of strings into a single string separated by a delimiter. # Example string string = "Hello, World!" # lower() method print(string.lower()) # Output: 'hello, world!' # upper() method print(string.upper()) # Output: 'HELLO, WORLD!' # split() method print(string.split(", ")) # Output: ['Hello', 'World!'] # replace() method print(string.replace("World", "Universe")) # Output: 'Hello, Universe!' # find() method print(string.find("World")) # Output: 7 # startswith() method print(string.startswith("Hello")) # Output: True # endswith() method print(string.endswith("!")) # Output: True # strip() method string_with_whitespace = " Hello, World! " print(string_with_whitespace.strip()) # Output: 'Hello, World!' # len() method print(len(string)) # Output: 13 # join() method words = ["Hello", "World"] #print(", ".join(words)) # Output: 'Hello, World' you can ¦nd more about strings methods here: https://docs.python.org/3/library/stdtypes.html#string-methods Exercise 1 Write a python function to get text as input and analyzes the given text. For example, for the following paragrph, it should output the word count, unique word count, average word length, most frequent word "Learning Python offers numerous bene¦ts for both beginners and seasoned developers. As a versatile and accessible language, Python boasts a syntax that's easy to grasp, making it an ideal entry point for those new to programming. Additionally, its wide range of applications—from web development to data analysis and arti¦cial intelligence—makes it indispensable in the modern tech landscape. The rich library ecosystem and supportive community further enhance its appeal, ensuring that Python learners have the tools and resources they need to solve complex problems and drive innovation. In essence, mastering Python can open doors to various technological realms and offer a competitive edge in the ever-evolving digital world." Code Text
9/14/23, 6:40 PM HW2- Fall 2023 (1).ipynb - Colaboratory https://colab.research.google.com/drive/16ADDoXrW3gW9qG83sEjo-4zHTcZahi1y#scrollTo=XLwbCtF2OCj9&printMode=true 2/3 Colab paid products - Cancel contracts here text = "Learning Python offers numerous benefits for both beginners and seasoned developers. As a versatile and accessible languag #your code here def func(text): text = text.replace("—", " ") words = text.split() word_count = len(words) unique_word = set(words) unique_word_count = len(unique_word) average_word_length = sum(len(word) for word in words) / len(words) most_frequent_word = '' num = 0 for word in words: count = words.count(word) if count > num: most_frequent_word = word num = count print(word_count) print(unique_word_count) print(average_word_length) print(most_frequent_word) func(text) 109 88 5.724770642201835 and Exercise 2 Write a function that takes a string as input and returns the longest word in the string. expectations ' ' #your code here strings = "I have big expectations for myself in the future." def function(strings): longest_word = '' for word in new_string: count = len(word) if count > len(longest_word): longest_word = word return longest_word string = strings.replace(".", " ") new_string = string.split() function(strings)
9/14/23, 6:40 PM HW2- Fall 2023 (1).ipynb - Colaboratory https://colab.research.google.com/drive/16ADDoXrW3gW9qG83sEjo-4zHTcZahi1y#scrollTo=XLwbCtF2OCj9&printMode=true 3/3 0s completed at 4:14 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