UTS BIG DATA & BUSINESS INTELLIGENCE
- Dapatkan link
- X
- Aplikasi Lainnya
Exerises
** Split this string:**
s = "Hi there Sam!"
**into a list. **
s = "Hi there Sam!"split_list = s.split()print(split_list)['Hi', 'there', 'Sam!']['Hi', 'there', 'Sam!']** Given the variables:**
planet = "Earth"
diameter = 12742
** Use .format() to print the following string: **
The diameter of Earth is 12742 kilometers.
planet = "Earth"diameter = 12742planet = "Earth"diameter = 12742output_string = "The diameter of {} is {} kilometers.".format(planet, diameter)print(output_string)The diameter of Earth is 12742 kilometers.
** Given this nested list, use indexing to grab the word "hello" **
lst = [1,2,[3,4],[5,[100,200,['hello']],23,11],1,7]nested_list = [1, 2, [3, 4], [5, [100, 200, ['hello']], 23, 11], 1, 7]# To access "hello," you'll need to use nested indexing:word = nested_list[3][1][2][0]print(word)hello
** Given this nested dictionary grab the word "hello". Be prepared, this will be annoying/tricky **
d = {'k1':[1,2,3,{'tricky':['oh','man','inception',{'target':[1,2,3,'hello']}]}]}nested_dict = { 'key1': 'value1', 'key2': { 'key3': 'value3', 'key4': { 'key5': { 'key6': 'hello' } } }}# Use nested keys to access the value "hello" in the dictionaryword = nested_dict['key2']['key4']['key5']['key6']print(word)hello
** What is the main difference between a tuple and a list? **
# List example (mutable)my_list = [1, 2, 3]my_list[0] = 4 # Modifying an elementmy_list.append(5) # Adding an elementmy_list.remove(2) # Removing an elementprint(my_list) # Output: [4, 3, 5]# Tuple example (immutable)my_tuple = (1, 2, 3)# The following line would result in an error:# my_tuple[0] = 4 # Trying to modify an element in a tuple[4, 3, 5]
** Create a function that grabs the email website domain from a string in the form: **
user@domain.com
So for example, passing "user@domain.com" would return: domain.com
def get_domain(email): # Split the email address at the '@' symbol to separate the user and domain parts = email.split('@') # Check if the email has the expected format with exactly one '@' symbol if len(parts) == 2: return parts[1] # Return the domain part else: return "Invalid email format"# Example usage:email = "user@domain.com"domain = get_domain(email)print(domain) # Output: "domain.com"domain.com
domainGet('user@domain.com')'domain.com'** Create a basic function that returns True if the word 'dog' is contained in the input string. Don't worry about edge cases like a punctuation being attached to the word dog, but do account for capitalization. **
def contains_dog(input_string): # Convert both the input string and the target word to lowercase for case-insensitive comparison input_string = input_string.lower() target_word = 'dog' # Check if the target word is in the input string return target_word in input_string# Example usage:input_string = "I have a Dog."result = contains_dog(input_string)print(result) # Output: TrueTrue
findDog('Is there a dog here?')True** Create a function that counts the number of times the word "dog" occurs in a string. Again ignore edge cases. **
def count_dog_occurrences(input_string): # Convert both the input string and the target word to lowercase for case-insensitive counting input_string = input_string.lower() target_word = 'dog' # Count the occurrences of the target word in the input string return input_string.count(target_word)# Example usage:input_string = "I have a dog. My friend has a dog too, and we love our dogs!"count = count_dog_occurrences(input_string)print(count) # Output: 33
countDog('This dog runs faster than the other dog dude!')2** Use lambda expressions and the filter() function to filter out words from a list that don't start with the letter 's'. For example:**
seq = ['soup','dog','salad','cat','great']
should be filtered down to:
['soup','salad']
seq = ['soup','dog','salad','cat','great']seq = ['soup', 'dog', 'salad', 'cat', 'great']filtered_words = list(filter(lambda word: word.startswith('s'), seq))print(filtered_words)['soup', 'salad']
Final Problem
**You are driving a little too fast, and a police officer stops you. Write a function to return one of 3 possible results: "No ticket", "Small ticket", or "Big Ticket". If your speed is 60 or less, the result is "No Ticket". If speed is between 61 and 80 inclusive, the result is "Small Ticket". If speed is 81 or more, the result is "Big Ticket". Unless it is your birthday (encoded as a boolean value in the parameters of the function) -- on your birthday, your speed can be 5 higher in all cases. **
def caught_speeding(speed, is_birthday): # Adjust the speed limit based on whether it's your birthday if is_birthday: speed_limit = 65 # 60 + 5 else: speed_limit = 60 # Check the speed and determine the result if speed <= speed_limit: return "No Ticket" elif speed <= (speed_limit + 20): # Speed limit + 20 mph return "Small Ticket" else: return "Big Ticket"# Example usage:speed = 70is_birthday = Falseresult = caught_speeding(speed, is_birthday)print(result) # Output: "Small Ticket"Small Ticket
caught_speeding(81,True)'Small Ticket'caught_speeding(81,False)'Big Ticket'Great job!
- Dapatkan link
- X
- Aplikasi Lainnya
Komentar
Posting Komentar