Question

In: Computer Science

Python 3.7: 1. Write a generator expression that repeats each character in a given string 4...

Python 3.7:

1. Write a generator expression that repeats each character in a given string 4 times. i.e. given “gone”, every time you call the next method on the generator expression it will display “gggg”, then “oooo”, … etc and instantiate it to an object called G.  G = (…..) # generatorExpression object

2. Test and verify that iter(G) and G are the same things. What does this tell you about the nature of a Generator object/expression?

3. Assign Iter(G) to an iterator object called it1 and advance it to the next element by using next method.

4. Instantiate a new Iter(G) object called it2. Use the next method and advance it to the next element. Observe the result and compare it with the results of it1.

5. Does the new Iter(G) object start the iterator from the start of the string object? Write down your understanding/conclusion from this experiment.

6. How can you get back the Iter(G) to point back to the beginning of the string?

Solutions

Expert Solution

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

Note: Please maintain proper code spacing (indentation), just copy the code part and paste it in your compiler/IDE directly, no modifications required.

#code

#defining a text to be used
text="gone"
#creating the required generator object that will return each character in text
#4 times in each iteration (part 1)

G=(i*4 for i in text)

#verifying that G and iter(G) are same (part 2) This proves that generators
#are in fact iterators. if you print G and iter(G), you can see that both
#objects point to the same object in same memory

print(G==iter(G)) #should be True
#creating an iterator for G and assigning to it1 (part 3)

it1=iter(G)
#printing next value of it1 (should be 'gggg')
print(next(it1))
#creating an iterator for G and assigning to it2 (part 4)
it2=iter(G)
#printing next value of it2 (should be 'oooo')
print(next(it2))

'''
5. Does the new Iter(G) object start the iterator from the start of the string object?
Answer: No it doesn't. This is because a generator can be iterated only once. Each iter()
calls return the same object in memory, so calling next on it2 will automatically call next
on it1 because both are same.

6. How can you get back the Iter(G) to point back to the beginning of the string?
Answer: The one and only way to do this is to re initialize the generator, pass the same
expression we used to create the generator (i*4 for i in text) in order to restart it.
if you put that expression inside a method, then we can just call that method whenever we
want to restart the generator. There is literally no other way.
'''

#output

True

gggg

oooo


Related Solutions

Using python: Given a string x, write a program to check if its first character is...
Using python: Given a string x, write a program to check if its first character is the same as its last character. If yes, the code should output "True"
IN PYTHON Given a number of petals, print a string which repeats the phrases "Loves me"...
IN PYTHON Given a number of petals, print a string which repeats the phrases "Loves me" and "Loves me not" for every alternating petal. The last phrase printed (for the last petal picked) should be in all caps. Remember to include a comma and space between phrases, as shown in examples below. Sample Input 1 3 Sample Output 1 "Loves me, Loves me not, LOVES ME" Sample Input 2 1 Sample Output 2 "LOVES ME"
Given an int variable k, write an expression whose value is the k-th character in the String variable word.
1. Given an int variable k, write an expression whose value is the k-th character in the String variable word. Assume thatword has been initialized and that the value ofk is non-negative and less than the length ofword.Example 1: if k's value is 3 andword is "spring", the value of the expression would be 'i'.Example 2: if k's value is 0 andword is "fall", the value of the expression would be 'f'.2. Given an int variable k, write an expression whose...
Given a String variable address, write a String expression consisting of the string "http://" concatenated with...
Given a String variable address, write a String expression consisting of the string "http://" concatenated with the variable's String value. So, if the variable refers to "www.turingscraft.com", the value of the expression would be "http://www.turingscraft.com". in java
To begin, write a program to loop through a string character by character. If the character...
To begin, write a program to loop through a string character by character. If the character is a letter, print a question mark, otherwise print the character. Use the code below for the message string. This will be the first string that you will decode. Use the String class method .charAt(index) and the Character class method .isLetter(char). (You can cut and paste this line into your program.) String msg = "FIG PKWC OIE GJJCDVKLC MCVDFJEHIY BIDRHYO.\n"; String decryptKey = "QWERTYUIOPASDFGHJKLZXCVBNM";...
a) Given a variable word that has been assigned a string value,write a string expression...
a) Given a variable word that has been assigned a string value, write a string expression that parenthesizes the value of word. So, if word contains "sadly", the value of the expression would be the string "(sadly)"b) Assume that price is an integer variable whose value is the price (in US currency) in cents of an item. Write a statement that prints the value of price in the form "X dollars and Y cents" on a line by itself. So,...
Write a recursive function to check if a string whose each character is stored in a...
Write a recursive function to check if a string whose each character is stored in a separate node in a doubly linked list, is a palindrome. Use the code available from DoublyLinkedList.hpp on our Github. // // Doubly-linked list with 2 dummy nodes // #pragma once #include <stdexcept> template<typename T> struct Node { T data; Node<T>* next; Node<T>* prev; Node() = delete; // Intentionally no default constructor Node( const T & element ) : data( element ), next( nullptr ),...
Phython: Given a string x, write a program to check if its first character is the...
Phython: Given a string x, write a program to check if its first character is the same as its last character. If yes, the code should output "True"
IN PYTHON Given a string with duplicate characters in it. Write a program to generate a...
IN PYTHON Given a string with duplicate characters in it. Write a program to generate a list that only contains the duplicate characters. In other words, your new list should contain the characters which appear more than once. Suggested Approach Used two nested for loops. The first for loop iterates from 0 to range(len(input_str)). The second for loop iterates from first_loop_index + 1 to range(len(input_str)). The reason you want to start at first_loop_index + 1 in the nested inner loop...
Using the windows32 framework, write a procedure to read a string and shift each character of...
Using the windows32 framework, write a procedure to read a string and shift each character of the string by one. As an example, if your input string is Abcz, your output should be Bcda. Note that you must test your string for non-alphabetic characters (such as numbers and special characters). If there are non-alphabetic characters, you should terminate your program with an appropriate message. You should display your converted string using the output macro. Hint: Let your procedure returns an...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT