Question

def zigzag(text): Implement a function that takes a string and returns a new string that is...

def zigzag(text): Implement a function that takes a string and returns a new string that is a zig-zag rearrangement of it like the one in the examples below.

• Parameters: text is a string (no validation needed)

• Examples:

zigzag('12345678') → '18273645'

zigzag('123456789') → '192837465'

zigzag('abcdefgh') → 'ahbgcfde'

zigzag('GeorgeMason') → 'GneoosragMe'

In Python please!

Homework Answers

Answer #1
def zigzag(s):
    i = 0
    j = len(s)-1
    res = ""
    while(i<j):
        res += (s[i]+s[j])
        i+=1
        j-=1
    if(len(s)%2==1):
        res += s[i]
    return res

# Testing
print(zigzag('12345678'))
print(zigzag('123456789'))
print(zigzag('abcdefgh'))
print(zigzag('GeorgeMason'))

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Implement function subset that takes a set of positive integers and returns asubset of it that...
Implement function subset that takes a set of positive integers and returns asubset of it that includes only those numbers that can form a sequence. It's ok if more than one sequences co-exist in the subset. You may not use lists, tuples, strings, dictionaries; only set functions/methods are allowed. Examples: subset({0,4,11,5,3,2,7,9})    ->    {4,5,3,2} subset({3,1,6,8,2,12,9})      ->    {3,1,2,8,9} True False In Python Please
Write a Python function that takes a filename as a parameter and returns the string 'rows'...
Write a Python function that takes a filename as a parameter and returns the string 'rows' if a file was written row by row, and 'columns' if the file was written column by column. You would call the function with a command like filetype = myfunc(filename).
Implement function reverse that takes a 2D list (a list of list of integers) and returns...
Implement function reverse that takes a 2D list (a list of list of integers) and returns a new 2D list where the items in each row are in reverse order. You maynot use slicing. You may not modify the input list. The returned list should be completely new in memory – no aliases with the input 2D list. From list methods, you may use only the .append(). Examples: reverse([[1,2,3],[4,5,6]])       ->    [[3,2,1],[6,5,4]] reverse([[1,2],[3,4],[5,6]])     ->    [[2,1],[4,3][6,5]] True False In python please
3. Implement the function read_matrix_file that consumes a file name (string) as its parameter and returns...
3. Implement the function read_matrix_file that consumes a file name (string) as its parameter and returns a nested list of ints (4x4 matrix) associated with the scores aligning G, A, T, and C. a. The nested list for the provided alignment_matrix text file would be: [[10,5,3,-5],[5,9,6,7], [3,6,12,8],[-5,7,8,10]]. Python language, txt file, sorry
Write a function named "replacement" that takes a string as a parameter and returns an identical...
Write a function named "replacement" that takes a string as a parameter and returns an identical string except with every instance of the character "i" replaced with the character "n python
This is function.py def processString(string): # Implement this function This is # This should print 4.5...
This is function.py def processString(string): # Implement this function This is # This should print 4.5 3.0 processString("An example. Two") # This should print 4.4 5.3 4.5 processString("This is the first sentence. The second sentence starts after the period. Then a final sentence") Download function.py and complete the processString(string) function. This function takes in a string as a parameter and prints the average number of characters per word in each sentence in the string. Print the average character count per...
Python: How would I write a function that takes a string representing a DNA sequence, and...
Python: How would I write a function that takes a string representing a DNA sequence, and return a string representing the complement ('A' and 'T' are complements of each other, as are 'C' and 'G'). The hints given were to use a dictionary to map a symbol A to its complement T, and to use join() rather than string addition. def dna_complement(text: str) -> str: assert(dna_complement('AAAACCCGGT') == 'ACCGGGTTTT') assert dna_complement('AcgTTTcAgCCC') == 'GGGCTGAAACGT'
Python Implement function allEven() that takes a list of integers and returns True if all integers...
Python Implement function allEven() that takes a list of integers and returns True if all integers in the list are even, and False otherwise. >>> allEven([8, 0, -2, 4, -6, 10]) True >>> allEven([8, 0, -1, 4, -6, 10]) False
Function which takes a string and uses a stack to calculate the value of theequation passed...
Function which takes a string and uses a stack to calculate the value of theequation passed in. This equation is assumed to be in postfix/reverse Polishnotation. RETURNS: The value that the equation evaluates to using postfix notation python please :)
Write a function that takes as input an English sentence (a string) and prints the total...
Write a function that takes as input an English sentence (a string) and prints the total number of vowels and the total number of consonants in the sentence. The function returns nothing. Note that the sentence could have special characters such as dots, dashes, and so on. write a python program please.
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT