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))
Related
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 9 months ago.
Improve this question
I'd like to get the digits of the Nth number by using the given numbers which are 3 and 9. From these numbers, I've got the numbers like 3, 9, 33, 39, 93, 99, 333 ... in order. So the digits of the 4th number is 2 and that of the 7th number is 3. What I want to do is to calculate the digits of the Nth number from this logic. Any suggestions? Thanks.
(You may assume that N is 1000000)
+What I've found was that there are 2 1-digit numbers (=2^1), 4 2-digit numbers (=2^2), and 8 3-digit numbers(=2^3). So I tried to apply the concept of geometric sequence but wasn't able to make it.
Written in binary, there are 10 numbers of a single digit, 110 numbers of two digits or less, 1110 numbers of three digits or less, 11110 numbers of four digits or less... So (2 << k)-2 numbers of k digits or less.
For a given N, it is an easy matter to find the smallest k that fits.
You already know that quantity of numbers with length k is 2^k, so we can exploit this fact. Essentially this is implementation of int(log2(n+1)) without any math libraries:
def nlen(n):
lng = 0
lcnt = 1
overall = 0
while overall < n:
lcnt *= 2
overall += lcnt
lng += 1
return lng
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.
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 6 years ago.
Improve this question
I have a dataset of phone numbers that I want to check against each other. Basically the regex should throw a match if two phone numbers are at most 1 digit apart. For example, we have the following phone numbers:
+31612345678
+31612245678
These numbers are the same apart from position number 7 (first number has a 3 while the second number has a 2). As these phone number differ by 1 digit, the regex should throw a match. It stands to reason that the regex should also throw a match if the phone numbers are exactly the same. In the following case (see below), the regex should however not throw at match as the phone numbers differ by more than 1 digit:
+31612345678
+31611145678
Does anyone have a good regex in mind? I am writing the regex using the re module in python.
Depending on your use case - if you want to also catch "oh, you missed a digit" or "eh, that digit shouldn't have been there", use the edit distance between the two numbers instead.
You can use the levenshtein edit distance to get a number for how many "edits" would be required between two numbers, for example by using the editdistance library for python.
>>> import editdistance
>>> editdistance.eval('banana', 'bahama')
2L
This may not be the best code, but it would do the job.
from collections import Counter
a = '+31612345678'
b = '+31612245678'
def match(p1, p2):
ct = Counter([a == b for a, b in zip(p1, p2)])
if not ct[False] > 1:
<throw match>
You wouldn't use a regular expression for this. If your phone numbers have the same length something simple as
def is_match(phone_nr_1, phone_nr_2):
diff = filter(lambda x: x[0] != x[1],
zip(phone_nr_1, phone_nr_2))
return len(diff) <= 1
print is_match("+31612345678", "+31612245678")
#=> True
print is_match("+31612345678", "+31611145678")
#=> False
would do the job.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I need a way to multiply all the numbers in a string by another number while keeping the structure and format of the string. Can it be done?
For example:
my_var = """Cat, 5, kg
Dog, 10, kg
Human, 50, kg"""
Given this string, I want to be able to multiply 5, 10 and 50 by another number saved in some other variable.
multiplier = 2.0/10 # or 0.2
So I want to use multiplier and my_var to create a new string:
"""Cat, 1, kg
Dog, 2, kg
Human, 10, kg"""
that I would then output to the user.
Any help would be appreciated!!
Regular expressions with a custom substitution function seem to suit the task well.
We can find all numbers in a string, convert them to integers and perform some arithmetic on them. After that they need to be converted back to strings to replace the former text.
import re
text = '''
Cat, 5 kg
Dog, 10 kg
Human, 50 kg
'''.strip()
def divide_numbers(s, n):
def sub(m):
return str(int(m.group(0))//n)
return re.sub('[0-9]+', sub, s)
print(divide_numbers(text, 5))
Cat, 1 kg
Dog, 2 kg
Human, 10 kg
Do notice that fractional parts are discarded everywhere here. You may want to use float instead of int and / instead of // (which is integer division).
Another interesting advantage of regular expressions here is you can make your pattern more advanced to match only some numbers. Here we match only the numbers that are followed by " kg", and I changed to float as mentioned
def divide_numbers(s, n):
def sub(m):
return str(float(m.group(1))/n)
return re.sub('([0-9]+) kg', sub, s)
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 8 years ago.
Improve this question
I'm beginning to learn the basics of python. I had just learned that str() turns non-strings into strings - example: str(2) would change 2 to "2". That raised a question - what is a string and what difference does it have from a non-string? I've googled this but I could not find this question is directly answered and the general explanations don't quite make it clear for me.
"That raised a question - what is a string and what difference does it have from a non-string?"
It sounds like python is your first language. That being said, for conceptual sake, a string is text, and a 'non-string' is a number. You will see why this is not quite true as you program more, but for understanding the difference between a string and a 'non-string' this will suffice. You can do math with 'non-strings'. "2" is a string, but 2 is a 'non-string'. Adding strings is NOT the same as arithmetic addition. "2" + "2" results in another string "22" (this operation is called concatenation ), but 2 + 2 results in a 'non-string' A.K.A. the NUMBER (not string) 4, because the addition is arithmetic addition.
A string is any sequence of characters — not just numbers, but letters and punctuation and all of Unicode.
Something that isn't a string is... not that. :) (There are lots of things that aren't strings! String isn't special.) For example, 2 is an int. You can do math on an int, because it's a number. But you can't do math on a str like "2"; it's only the way we write the number in Western mathematics, not the number itself. You couldn't ask "dog" to wag its tail, either, because it's not a real dog; it's just the written word "dog".
As a more practical example:
2 + 2 gives you 4, the result of combining two numbers.
"2" + "2" gives you "22", the result of combining two written "words".
just to put another spin on this...
objects in python come with a variety of attributes and methods. attributes tend to represent data associated with the object. methods tend to represent behaviors that can be performed by the object. if we create a string and give it the name a and look at the list of attributes/methods, we see that the list encompasses many of the things you would want to know about a string or do with a string.
In [91]: a = '1' # assign a string the name 'a'
In [92]: a.
a.capitalize a.format a.isupper a.rindex a.strip
a.center a.index a.join a.rjust a.swapcase
a.count a.isalnum a.ljust a.rpartition a.title
a.decode a.isalpha a.lower a.rsplit a.translate
a.encode a.isdigit a.lstrip a.rstrip a.upper
a.endswith a.islower a.partition a.split a.zfill
a.expandtabs a.isspace a.replace a.splitlines
a.find a.istitle a.rfind a.startswith
on the other hand, if we create a number and give it the name b and look at the list of attributes/methods, we see that they are very different and focuses on things we would want to know about a number or do with a number.
In [92]: b = 1 # assign a number the name 'b'
In [93]: b.
b.bit_length b.denominator b.numerator
b.conjugate b.imag b.real