Question

Hi, I have Python programming problem as follow:

Write a function solution that, given two integers A and B, returns a string containing exactly A letters a and exactly B l

Thank you.

Best Regards.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Python:


def solution(A,B):
str1="" # string will contain the answer string.
if A>B: #A greater than B
while A/2<B and A>0 and B>0:
str1=str1+"ab" #concat the 'ab'
A=A-1
B=B-1
while A/2>=B and A>0 and B>0:
str1=str1+"aab" #concat the 'aab'
A=A-2
B=B-1
elif B >= A: #B greater than A
while B/2<A and A>0 and B>0:
str1=str1+"ba" #concat the 'ba'
A=A-1
B=B-1
while B/2>=A and A>0 and B>0:
str1=str1+"bba" #concat the 'bba'
B=B-2
A=A-1
  
while A>0 and B<=0:
str1=str1+'a' #concat 'a' if B less than equal 0
A=A-1
while B>0 and A<=0:
str1=str1+'b' #concat 'b' if A less than equal 0
B=B-1
return str1   

print (solution(5,3)) #print the string
print (solution(3,3)) #print the string
print (solution(1,4)) #print the string

Output:

abaabaab bababa bbabb

Add a comment
Know the answer?
Add Answer to:
Hi, I have Python programming problem as follow: Thank you. Best Regards. Write a function solution...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Hi, I have programming problem related to array, sorting, and. swap operation Thank you, Best Regards.....

    Hi, I have programming problem related to array, sorting, and. swap operation Thank you, Best Regards.. A non-empty array A consisting of N integers is given. You can perform a single swap operation in array A. This operation takes two indices I and J, such that 0 S13 J<N, and exchanges the values of A[i] and A[J]. The goal is to check whether array A can be sorted into non-decreasing order by performing at most one swap operation. For example,...

  • PYTHON 3!!!!! Write a function: class Solution { public String solution(String T); } that, given a...

    PYTHON 3!!!!! Write a function: class Solution { public String solution(String T); } that, given a string T, returns the latest valid time that can be obtained from T, as a string in the format "HH:MM", where HH denotes a two-digit value for hours and MM denotes a two-digit value for minutes. Examples: 1. Given T = "2?:?8", the function should return "23:58". 2. Given T = "?8:4?", the function should return "18:49". 3. Given T = "??:??", the function...

  • Hi, I have Java programming problem: Please solve using Java. Thanks. Best Regards. In the army,...

    Hi, I have Java programming problem: Please solve using Java. Thanks. Best Regards. In the army, each soldier has an assigned rank. A soldier of rank X has to report to (any) soldier of rank X + 1. Many soldiers can report to the same superior. Write a function: class Solution { public int solution(int[] ranks); } that, given an array ranks consisting of soldiers' ranks, returns the number of soldiers who can report to some superior. Examples: 1. Given...

  • Hi, I need help in solving Python programming problem below: Thank you. Best Regards When John...

    Hi, I need help in solving Python programming problem below: Thank you. Best Regards When John gambles at the casino, he always uses a special system of tactics that he devised himself. It's based on always betting in one of two ways in each game: • betting exactly one chip (to test his luck); • betting all-in (he bets everything he has). Wins in the casino are paid equal to the wager, so if he bets C chips and wins,...

  • Write a function: that, given two non-negative integers A and B, returns the number of bits...

    Write a function: that, given two non-negative integers A and B, returns the number of bits set to 1 in the binary representation of the number A * B. For example, given A = 3 and B = 7 the function should return 3, because the binary representation of A * B = 3 * 7 = 21 is 10101 and it contains three bits set to 1. Assume that: In your solution, focus on correctness. The performance of your...

  • i need python code, thank you very much! characters) and prints ) 3. Write function, smallestCharacterLargerThan(keyChar,...

    i need python code, thank you very much! characters) and prints ) 3. Write function, smallestCharacterLargerThan(keyChar, inputString) that takes as input key character, keyChar, and a string of one or more letters (and no other the "smallest" character in the string that is larger than keyChar, where one character is smaller than another if it occurs earlier in the alphabet (thus 'C is smaller than 'y' and both are larger than 'B') and 2) the index of the final occurrence...

  • Please write the code in a text editor and explain thank you! 1. LINKED-LIST: Implement a...

    Please write the code in a text editor and explain thank you! 1. LINKED-LIST: Implement a function that finds if a given value is present in a linked-list. The function should look for the first occurrence of the value and return a true if the value is present or false if it is not present in the list. Your code should work on a linked-list of any size including an empty list. Your solution must be RECURSIVE. Non-recursive solutions will...

  • IN PYTHON CODE Question #1 Write a function capital that has one argument: strlist that is...

    IN PYTHON CODE Question #1 Write a function capital that has one argument: strlist that is a list of non-empty strings. If each string in the list starts with a capital letter, then the function should return the value True. If some string in the list does not start with a capital letter, then the function should return the value False You may use any of the string functions that are available in Python in your solution, so you might...

  • WE ARE USING PYTHON TO COMPLETE THIS ASSIGNMENT :) THANK YOU! In this programming assignment, you...

    WE ARE USING PYTHON TO COMPLETE THIS ASSIGNMENT :) THANK YOU! In this programming assignment, you will write functions to encrypt and decrypt messages using simple substitution ciphers. Your solution MUST include: a function called encode that takes two parameters: key, a 26-character long string that identifies the ciphertext mapping for each letter of the alphabet, in order; plaintext, a string of unspecified length that represents the message to be encoded. encode will return a string representing the ciphertext. a...

  • This is in C++. (10 points) Write a de-duplication function that iteratively sanitizes (removes) all consecutive...

    This is in C++. (10 points) Write a de-duplication function that iteratively sanitizes (removes) all consecutive duplicates in a C++ string. Consecutive duplicates are a pair of duplicate English alphabets sitting next to each other in the input string. Example: "AA", "KK", etc., are all consecutive duplicates. This function will internally run as many iterations as needed to remove all consecutive duplicates until there is either no consecutive duplicates left, or the string becomes empty (in which the function returns...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT