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
Related
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 11 months ago.
Improve this question
given the number 12345 = 1x10^4 + 2x10^3 + 3x10^2 + 4x10^1 + 5x10^0, how to perform some arithmetic operations to leave you with just the digit at position 5 (from the left) and output it to the screen? Thanks for the help!
Assuming you want to stick to arithmetic operations (and not strings), use the modulo operator with 10 to get the remainder of division by 10, i.e. the unit:
12345%10
output: 5
For an arbitrary number, you need to compute the position, you can use log10 and ceil:
from math import log10, ceil
N = 5
number = 1234567
number//10**(ceil(log10(number))-N)%10
output: 5
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 1 year ago.
Improve this question
Why my output result doesn't show 0 when it's supposed to show exact number in reverse order in Python.Without changing the parameter variable to string.
parameter = int(input())
sum = 0
while parameter > 0:
digit = parameter % 10
sum = sum * 10 + digit
parameter = parameter // 10
print(sum)
A couple of things are going on here. First, you're converting the input to an int. This means that when reversing the number you'll lose all trailing 0s. This is because for an int there is no reason to show a leading 0. Instead, you can simply print out each digit in reverse. Another thing to note is you shouldn't use sum as a variable since it will overwrite the built in sum function.
parameter = int(input())
while parameter > 0:
digit = parameter % 10
print(digit, end='')
parameter = parameter // 10
print("")
You can do this by printing the output right-justified in a field of the same width as the original number. This doesn't really avoid the fact that string manipulation is required, but at least it allows you to store the number as a number and not a string:
from math import ceil, log10
...
digits = ceil(log10(parameter + 1)) or 1
...
print(f'{sum:>0{digits}}')
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
How can I get the fractional part of a number?
For example, I have a list of floats num = [12.73, 9.45] and want to get only the numbers after the decimal point, 73 and 45 in this case. How do I go about doing this?
One approach is using pure(ish) maths.
The short answer:
num = [12.73, 9.45]
[int((f % 1)*100) for f in num]
>>> [73, 44]
Explanation:
The modulo operator returns the remainder once whole division is complete (to over-simplify).
Therefore this, returns the decimal value; the fractional part of the number.
12.73 % 1
>>> 0.7300000000000004
To get the decimal value as a integer, you can use:
int((12.73 % 1)*100)
>>> 73
Just wrap this in a loop for all required values ... and you have the 'short answer' above.
num = [12.73, 9.45];
result = list(map(lambda x: int(str(x).split('.')[1]),num))
print(result)
and want to get only the numbers after the period,
There is no such thing. Numbers don't have digits; the string representation of the numbers has digits. And even then, floating-point numbers are not precise; you may be shown 0.3 in one context and 0.30000000000000004 in another, for the same value.
It sounds like what you are actually after is the fractional part of the numbers. There are many ways to do this, but they all boil down the same idea: it is the result when you divide (as a floating-point number) the input by 1.
For a single value, it looks like:
fractional_part = value % 1.0
or
# This built-in function performs the division and gives you
# both quotient and remainder.
integer_part, fractional_part = divmod(value, 1.0)
or
import math
fractional_part = math.fmod(value, 1.0)
or
import math
# This function is provided as a special case.
# It also gives you the integer part.
# Notice that the results are the other way around vs. divmod!
fractional_part, integer_part = math.modf(value)
To process each value in a list in the same way, use a list comprehension.
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))
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.