![nickonicyetlan](/avatars/32792.jpg)
nickonicyetlan
17.12.2019 •
Computers and Technology
We consider a string with only alphabets 'a', 'b', 'c'. a string is diverse if no three consecutive letters are the same. meaning that a diverse string cannot contain strings 'aaa', 'bbb', 'ccc'.
write the most efficient python program that given three integers a,b and c, it returns any longest diverse string containing at most a 'a's, b 'b's, and c 'c's. if generating a string is impossible return a blank string.
eg.
given a=6, b=1, c=1
the program may return aabaacaa. note that aacaabaa is also valid.
given a=0, b=1, c=8
the only correct string is ccbcc
Solved
Show answers
More tips
- A Auto and Moto How to Start a Diesel Engine in Cold Weather?...
- F Family and Home How to Remove Tar Stains: Tips and Recommendations from Experts...
- F Family and Home How to Remove Fading from Clothes: Tips and Tricks...
- S Sport How to Do a Jumping Split...
- H Health and Medicine How Did Inna Lose Weight on Dom 2?...
- F Family and Home How to Properly Fold Napkins in a Napkin Holder?...
- F Food and Cooking How to Set Up Ventrilo - The Ultimate Guide...
- S Science and Technology How to Make a Homemade Smoker: The Ultimate Guide...
- A Auto and Moto Battle for the Relocation of The Cherkizovsky Market: Who Won?...
- C Computers and Internet How Do You Refill Cartridges?...
Answers on questions: Computers and Technology
- C Computers and Technology Lorraina is editing a novel. Which style guide is the publisher most likely going to ask her to use? American Psychological Association (APA) Associated Press (AP)...
- C Computers and Technology Given the following code segment, int x = 50; int y = 15; int z = x / y; what is the value assigned to z?...
- C Chemistry An element has six valance electrons available for bonding. which group of the periodic table does this element most likely belong to?...
- H Health You are caring for a client status post lung resection. when assessing your client you find that the bubbling in the water-seal chamber for the chest tubes is more...
- P Physics An isolated conducting sphere has a 8.2 cm radius. one wire carries a current of 1.32 a into it while another wire carries a current of 1. a out of it. how long...
- B Business What’s the difference between a commercial bank and an investment bank? list the major types of financial institutions, and briefly describe the primary function...
- S Social Studies In a science lesson on heat, ms. jones explains that heat is the result of molecules moving back and forth very quickly and that gases are heated more quickly than...
- B Business In contemplating the launch of her seafood restaurant, paige graham concluded that suppliers of seafood had an advantage in bargaining power over restaurant owners...
- P Physics The trajectory of a projectile always the trajectory of a projectile always is a straight line, in the same direction as the initial velocity vector follows a circular...
- B Business Crane companywants to produce and sell a new flavored water. in order to penetrate the market, the product will have to sell at $2 per 12 oz. bottle. the following...
Ответ:
from itertools import permutations
import math
import re
a = int(input())
b = int(input())
c = int(input())
num = math.ceil((a + b + c) / 2)
str_val = a*'a'+b*'b'+c*'c'
str_per = list(set(list(permutations(str_val
temp_str, output_str = [], []
val = ["".join(i) for i in str_per]
for each in val:
all_find_str = re.findall(r"([a-z])\1{2,}", each)
if not all_find_str:
output_str.append(each)
else:
all_find_str = re.findall(r"([a-z])\1{2,}", each[:num])
if not all_find_str:
temp_str.append(each[:num])
if output_str:
print((output_str[0]))
else:
print("Output is:", set(temp_str))
Explanation:
We first need to have all necessary modules imported.
This is a permutation related question and we will be using the permutation module. The math and regular expression module is also imported.
The program begins by getting the user inputs for a, b, c.
A permutation is ran based on the values of the letters. Permutations work by finding the number of ways the strings can be joined.
If the permutation is more than one, the first index is the default output. If a single string is generated, it will be the output. A blank string will be the output if generating a string is impossible.
Ответ:
Yes it's very true