Computer Science, asked by fiercespartan, 11 months ago

Given a string of a length size up to 100, and given a number which has no limit.
For example,

string = 'cac'
number = 10

We will have to multiply the string by n number of times until the length size of our string would match the number.

In the given example, to get length 10, our string has to look like this:

caccaccacc : cac | cac | cac | c
After that, we need to print the output of the number of a's in the string.

Output - 3 as caccaccacc has 3 a's

Here are some examples:

string = 'aaswesgttesg'
number = 234567
OUTPUT - 39096

____________________

string = 'abcdefg'
number = 14
OUTPUT - 2

____________________

string = 'aaaaa'
number = 21

OUTPUT - 21

____________________

string = 'ababa'
number = 7

OUTPUT - 4
___________________

Here is the starting of the code:

string, number = input(), int(input())
_______________________

You can take however many lines to solve it but try doing it in just one line :)

Answers

Answered by QGP
7

Counting Substrings - Python

The given problem reduces to counting of the occurences of a substring inside a given string.

Let's break this problem down. We have a string, which we need to keep multiplying until it reaches a given length. The end part of the string might get truncated.

This is similar to the process of division and getting quotient and remainder.

For example, consider this:

string = 'cac' (length = 3)

number = 10

We have: 10 = 3 \times \bold{3}\ + 1

Similarly, for

string = "aaswesgttesg" (length = 12)

number = 234567

We have: 234567 = 19547 \times \bold{12}\ + 3

Remember the quotient and remainder part. The remainder part shows the length of the string that will be left after truncating.

Now, we need to count the number of "a"s in the string.

What we can do is, count the number of such "a"s in the original string, multiply it by the quotient.

To this, add the number of "a"s in the final truncated string added at the end. This will easily give us the total number.

 \rule{320}{1}

The count() method

Python provides a nice function for our use.

The str.count(substring, start, end) returns the count of the substring in the string str from start index to end index. The start and end parameters are optional though.

We can use this method with the integer and modulo division operators to accomplish our function.

Here's a sample program.

 \rule{320}{1}

# User input of string and number

string, number = input(), int(input())

# Extract the length of string for further use

length = len(string)

basecount = (number // length) * string.count("a")

# The additionalcount represents the number of "a"s in the remainder string

additionalcount = string.count("a", 0, number % length)

totalcount = basecount + additionalcount

print(totalcount)

 \rule{320}{1}

As for making the program computation a single line, we can do something like this after the input.

string, number = input(), int(input())

print((number // len(string)) * string.count("a") + string.count("a", 0, number % len(string)))

It will return the same output.

Attachments:
Answered by Anonymous
2

Answer:

Let's break this problem down. We have a string, which we need to keep multiplying until it reaches a given length. The end part of the string might get truncated.

Remember the quotient and remainder part. The remainder part shows the length of the string that will be left after truncating.

What we can do is, count the number of such "a"s in the original string, multiply it by the quotient.

Now, we need to count the number of "a"s in the string.

This is similar to the process of division and getting quotient and remainder.

ANSWER

number = 234567

For example, consider this:

string = "aaswesgttesg" (length = 12)

Similarly, for

number = 10

string = 'cac' (length = 3)

234567 = 19547 \times \bold{12}\ + 3

10 = 3 \times \bold{3}\ + 1

Computer Science

We have:

We have:

50 points

Similar questions