User-defined function takes input in the form of xx(s)


Sakovic

I have defined a function to return something from a string. I can now get the desired output with a specific input, but I am not sure how to achieve the correct output given the desired input form. I had to do this in Python 2.7.13

The input format I am asked to use is the following:

Import: solution.solution (“abccbaabccba”)

Output: The number of times the string pattern is repeated.

The function is defined as follows:

s = "abccbaabccba"

def solution(s): 
    distinctChars = len(set(s))
    lengthString = len(s)

    patternLen1 = int(distinctChars) 
    pattern1Search = s[:patternLen1]

    upperPossibleReps = int(lengthString / distinctChars)

    matchTryUpper = pattern1Search * upperPossibleReps

    patternLen2 = int(distinctChars * 2)
    pattern2Search = s[:patternLen2]

    lowerPossibleReps = lengthString / (distinctChars * 2)

    matchTryLower = pattern2Search * int(lowerPossibleReps)

    if matchTryUpper == s:
        solution = int(upperPossibleReps)
        print(solution)
    else:
        solution = int(lowerPossibleReps)
        print(solution)

solution(s)
    
Andreas
s = "abccbaabccba"

def repeating_pattern(s):
    a = s[:int(len(s)/2)]
    b = s[int(len(s)/2):]
    winner = ""
    while len(a) != 0:
        if a in b:
            if len(s.replace(a, ""))==0:
                winner = a
        a = a[:-1]
    return winner

repeating_pattern(s)

Related


How to give user defined function as user input in python?

Manas Dogra I am trying to write a program to define a user defined function and use the return expression as the user's input. Suppose a function should return x + y. In python we can do this - def f(x,y): return x+y However, I want something like a=input("

User defined function with TEXT input

Casboy After some projects were approved, I had monthly reports that I started running in R. I want to be able to manually start a few lines of code. I believe this is best done with a user defined function, but I'm not sure where to start. Here is an example

A function that takes arguments as user input

big daddy I am trying to write a program that requires user input 2 times. location of text file containing array and text A column in an array. Then I want to extract that column from the array and perform a function on it using my own module stats. I have tr

User defined function in r that takes color as input

hm998 I am trying to create a function that plots data It has 3 inputs, (dataframe, string, string) I am struggling with the last string. So imagine my function called plot_by_city(dataframe, "Amsterdam", "springgreen4") Then I want it to generate my plot some

Is it possible to get user's input from a user-defined function?

John Papa Below is the code to add two numbers. I ask for user input and validate in function GetValidateInput(). The code works fine, but is it the correct way to request user input from a user defined function or should we do this in Main() and only validate

How to give user defined function as user input in python?

Manas Dogra I am trying to write a program to define a user defined function and use the return expression as the user's input. Suppose a function should return x + y. In python we can do this - def f(x,y): return x+y However, I want something like a=input("

User defined function seems to double past input

py_newb_217 With the code I'm currently setting up, the value seems to double and I can't figure out why. For example I'll run an array (E) through a function, [5.569, 10.9536, 16.167, ...]then when I run the function later with the array (G), suddenly (E) wil

User-defined function takes input in the form of xx(s)

Sakovic I have defined a function to return something from a string. I can now get the desired output with a specific input, but I am not sure how to achieve the correct output given the desired input form. I had to do this in Python 2.7.13 The input format I

User defined function in r that takes color as input

hm998 I am trying to create a function that plots data It has 3 inputs, (dataframe, string, string) I am struggling with the last string. So imagine my function called plot_by_city(dataframe, "Amsterdam", "springgreen4") Then I want it to generate my plot some

Take a user-defined range as input to a function

username I am relatively new to VBA in Excel. I want to write a custom function that accepts a user defined range as input and then searches that range for a specific word. So far, the following code works, but it only searches in the predefined range "a2:g2",

Use discord.py to make a function that takes input from the user?

Miklath I want to create a function in python that replicates the input() function using the discord.py module. I've seen posts about using it, client.wait_for('message', check=lambda m: m.user == ctx.user)but I don't know how to implement it. Here is some pse

User-defined function takes input in the form of xx(s)

Sakovic I have defined a function to return something from a string. I can now get the desired output with a specific input, but I am not sure how to achieve the correct output given the desired input form. I had to do this in Python 2.7.13 The input format I

User defined function in r that takes color as input

hm998 I am trying to create a function that plots data It has 3 inputs, (dataframe, string, string) I am struggling with the last string. So imagine my function called plot_by_city(dataframe, "Amsterdam", "springgreen4") Then I want it to generate my plot some

User defined function with TEXT input

Casboy After some projects were approved, I had monthly reports that I started running in R. I want to be able to manually start a few lines of code. I believe this is best done with a user defined function, but I'm not sure where to start. Here is an example

User defined function seems to double past input

py_newb_217 With the code I'm currently setting up, the value seems to double and I can't figure out why. For example I'll run an array (E) through a function, [5.569, 10.9536, 16.167, ...]then when I run the function later with the array (G), suddenly (E) wil

A function that takes arguments as user input

big daddy I am trying to write a program that requires user input 2 times. location of text file containing array and text A column in an array. Then I want to extract that column from the array and perform a function on it using my own module stats. I have tr

User-defined function takes input in the form of xx(s)

Sakovic I have defined a function to return something from a string. I can now get the desired output with a specific input, but I am not sure how to achieve the correct output given the desired input form. I had to do this in Python 2.7.13 The input format I

User-defined function takes input in the form of xx(s)

Sakovic I have defined a function to return something from a string. I can now get the desired output with a specific input, but I am not sure how to achieve the correct output given the desired input form. I had to do this in Python 2.7.13 The input format I

User-defined function takes input in the form of xx(s)

Sakovic I have defined a function to return something from a string. I can now get the desired output with a specific input, but I am not sure how to achieve the correct output given the desired input form. I had to do this in Python 2.7.13 The input format I

User defined function in r that takes color as input

hm998 I am trying to create a function that plots data It has 3 inputs, (dataframe, string, string) I am struggling with the last string. So imagine my function called plot_by_city(dataframe, "Amsterdam", "springgreen4") Then I want it to generate my plot some

User defined function in r that takes color as input

hm998 I am trying to create a function that plots data It has 3 inputs, (dataframe, string, string) I am struggling with the last string. So imagine my function called plot_by_city(dataframe, "Amsterdam", "springgreen4") Then I want it to generate my plot some