Finding pairs of symbols in the end of a string [closed] - python

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I have to write a program that returns ...
-1 for an empty string
0 for a single-character string
next-to-last index if the final two characters match
last index if they are different
For instance:
"tara" => 3
"baa" => 1
"adjsk" => 4
"sthkk"=> 3
"a" => 0
It seems that I'm returning index of last character wrongly:
def ends_with_pair(s):
for i in range(len(s)-1):
if s[i] == s[i+1]:
return s.index(s[i])
return s.index(s[-1])
Also, is there a way to make it more compact?

Your logic is far too complex. The problem involves only the final two characters; there's no need to loop through the string.
Check the string length; if it's 0, return -1. If it's 1, return 0.
Check the last two characters against each other s[-1] == s[-2]. If they're equal, return len(s)-2; else return len(s)-1.
I trust you can turn that into code.

Related

Hi. Why am I getting "NameError: name 'number' is not defined " for this code? Please help me here as I have no idea [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 months ago.
Improve this question
alphabet=str(input("Enter the alphabet: "))
if "A"==1 and "B"==2 and "C"==3 and "D"==4 and "E"==5 and "F"==6 and "G"==7 and "H"==8 and "I"==9 and "J"==10 and "K"==11 and "L"==12 and "M"==13 and "N"==14 and "O"==15 and "P"==16 and "Q"==17 and "R"==18 and "S"==19 and "T"==20 and "U"==21 and "V"==22 and "W"==23 and "X"==24 and "Y"==25 and "Z"==26:
number=alphabet
print("The number of the alphabet is",number)
Your expressions return false so number is not defined. add
number = " string is not equal to a number."
before the expression.
I think your trying to get a number value for each character? you should be comparing if the value of alphabet equals a given string, then assigning a numerical value to number.
I.E.
if alphabet.upper() == "A": number=1

What can be the solution of lapindrome problem as stated below [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I am new to competitive programming. I am solving the below problem and stuck
Lapindrome is defined as a string which when split in the middle,
gives two halves having the same characters and same frequency of each
character. If there are odd number of characters in the string, we
ignore the middle character and check for lapindrome. For example
gaga is a lapindrome, since the two halves ga and ga have the same
characters with same frequency. Also, abccab, rotor and xyzxy are a
few examples of lapindromes. Note that abbaab is NOT a lapindrome.
The two halves contain the same characters but their frequencies do
not match. Your task is simple. Given a string, you need to tell if it
is a lapindrome.
Input:
First line of input contains a single integer T, the number of test cases.
Each test is a single line containing a string S composed of only lowercase English alphabet.
Output:
For each test case, output on a separate line: "YES" if the string is a lapindrome and "NO" if it is not.
Constraints:
1 ≤ T ≤ 100
2 ≤ |S| ≤ 1000, where |S| denotes the length of S
Example:
Input:
6
gaga
abcde
rotor
xyzxy
abbaab
ababc
Output:
YES
NO
YES
YES
NO
NO
Can you give me the solution of this problem with the logic behind the solution. (language=python)
a="abbaab"
def lapindrome(a):
l = int(len(a)/2)
return "YES" if sorted(a[:l]) == sorted(a[-l:]) else "NO"
print(lapindrome(a))

How to calculate the fraction of a certain letter within a string? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
Hi I would like to know how I can calculate the fraction of certain letters in a string. Like for example if I have the string "ABGTACTGASDJASBFGJAFKA" and want to calculate the fraction of A+D in that string what would I have to do? I want to do this without having to import anything and only using the built in functions of python.
def fraction(string):
A = ""
D = ""
A = string.count("A")
D = string.count("D")
print(int(A/D * 100), "%")
A/D is not a the fraction of a certain letter in the whole sting.
The following line will calculate the percentage of "A" in your string.
100.0 * ( string.count("A") / len(string) )
My suggestion, don't do both characters at once. Start with this
def fraction(string, letter):

PyCharm Tutorial , While Loop [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
square = 0
number = 1
while number < 10:
square = number ** 2
print(square)
number += 1
It's my answer for this question :" Print all squares from 0 to 99(1,4,..,81)Use number variable in while loop."
Pycharm says it doesn't match with its answer.
I think i should print those numbers in a single line but i couldn't deal with it.How can i do that ?
Your code will print each number in a newline, because, in Python, the call to print() comes with an implicit newline (see the end argument per the documentation).
Next, you're making an assumption about output formatting. As I see it, there are two primary issues with this assumption:
1) Calls to functions (e.g. print()) in a while loop "execute" when they're called -- there's no delay to see if a future pass through the loop will provide extra data to the function.
2) You're assuming that the Python interpreter will guess that printed numbers (in a while loop) are desired to be returned in a comma separated list. Computers are machines that do what you tell them to do -- if you don't write logic to explain what you need, the machine cannot give you this.
You can express your desired output in the following ways:
1) Collect the numbers (as strings) in a list, then output them after you're done looping:
square = 0
number = 1
# think of this as a result container
number_result_list = []
while number < 10:
square = number ** 2
# place that number at the end of your container:
number_result_list.append(str(square))
number += 1
# join all your number strings together, using a comma
print(",".join(number_result_list))
# prints 1,4,9,16,25,36,49,64,81
2) Specify that you want to use a comma in the call to print. Make special note of the trailing comma -- you now know why this happens:
square = 0
number = 1
while number < 10:
square = number ** 2
print(square, end=",")
number += 1
# prints 1,4,9,16,25,36,49,64,81,

How to anagram digit in python? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
What is my problem ? I want act inverse number--example : 123 ==>321
def loop(a):
i=0
while(a>=1)
print(a%10)
s=s/10
i=i+1
Your solution has a few problems, aside from the indentation and the missing colon.
First of all your are using print which automatically adds a line break, so that might not be what you want the result to look like. You could store the result in a string which you append the latest character to and then print it once at the end.
Further, you are using a variable s which was never used before. In thise case it should be a as you want to strip off the last digit using an integer division by 10. Note that in this case, this will only work like that in Python 2, as Python 3 will use a float division there (e.g. 15 / 10 == 1.5). You can prevent that by explicitly using the integer division there (this will also make your intend more clear): s = s // 10 (note the two slashes).
Lastly, you are incrementing the variable i without ever using it, so you can just get rid of it.
In the end, it might look like this:
def reverse (a):
rev = ''
while a > 1:
rev += str(a % 10)
a = a // 10
A shorter solution, utilizing the fact that you can just reverse strings:
>>> num = 123
>>> rev = int(str(num)[::-1])
>>> rev
321
If you leave out the int(), you can even keep trailing/leading zeros and get a string instead:
>>> num = 3210
>>> str(num)[::-1]
'0123'
Few issues:
Your indentation does not match. PEP 8 suggests 4 spaces for indentation.
You're missing a colon after while(a>=1)
Although this isn't an issue, you don't need the parentheses in the while loop, it can just be while a >= 1
s = s/10 might not return what you expect. For example, 12/10 == 1 (unless you're dealing with floats here).
This can all be simplified using slicing:
>>> print int(str(123)[::-1])
321
It is important to indent correctly. (And don't mix tabs and spaces.)
def loop(a):
i = 0
while a >= 1:
print(a % 10)
a = a / 10
i = i + 1
You were also missing a colon after the while condition.

Categories

Resources