Number conversion in Python 2.7: base6 to base10 - python-2.7

Searched about this and didn't find useful help. Maybe I searched wrong. Figured I might just ask here and find out more by that.
Let's say I have 99 numbers with a range from 1 - 6 made from rolling a 6 sided dice 99 times.
And I want to convert this from base6 (is this already base6 format?) to base10 in python 2.7, how would I do it? Code should be non cryptic and readable.
Automatic replacement of 6's to 0's would be good to, if needed.
It's for Bitcoin Private Key generation by the way.
The dice rolls are being entered in manually right when it asks "Enter dice rolls: "
Basicly converting 6 sided dice rolls from number range 1 - 6 -> 0 - 5 -> 0 - 9
Don't laugh, basicly what I have written now is just this:
import ...????
dice_input = input("Enter dice rolls: ")
dice_convert_base6_to_base10 = base10.encode(dice_input)...????
No idea if or what I would need to import and what to do with the dice_input. My question may sound a bit stupid to most of you, please don't judge.. Thanks a lot for every help I can get!

Use the built-in int function doc.
It does conversion from string to integer types. So, you will need to get the input as a string doc.
It accepts a second (optional) parameter of the base you want to assume the source comes from.
Code would end up looking something like:
dice_input = raw_input("Enter dice rolls: ")
dice_convert_base6_to_base10 = int(dice_input, 6)
To convert any 6 to 0, then you could just use the replace method of the string doc.
Code would then be changed to something like:
dice_input = raw_input("Enter dice rolls: ").replace('6','0')
dice_convert_base6_to_base10 = int(dice_input, 6)
Not certain about if this is really the way to get the key generation you want... but, I don't think I really grokked the use-case.

Related

Ignore any number in the tenth position in Open Offic Calc

I am trying to right a script for a current project but I need it to note display the number in the tenth position.
For example,
A1 = 9
A2 = 3
=SUM(A1+A2)
Would be
12
How ever how can I get it to display only 2?
The numbers will change with each entry. Sometimes it could be 9+45= 54 however I would want it to display only 4. Not sure how I would go about making it take away the number only in the tenth position.
You probably want something like:
=MOD(A1+A2,10)
Though I believe OpenCalc will use the semicolon as a parameter delimiter.
You can trasnform number to string then take last number. Something like this:
C1 = SUM(A1 + A2)
RIGHT(TEXT(C1,0),1)
The result is
2
You are looking for the Units position, not the Tens. The Units is always the furthest right character so just extract it right from there. No need to convert to string first
=RIGHT(SUM(A1:A2),1)

Regex for binary multiple of 3

I would like to know how can I construct a regex to know if a number in base 2 (binary) is multiple of 3. I had read in this thread Check if a number is divisible by 3 but they dont do it with a regex, and the graph someone drew is wrong(because it doesn't accept even numbers). I have tried with: ((1+)(0*)(1+))(0) but it doesn't works for some values. Hope you can help me.
UPDATE:
Ok, thanks all for your help, now I know how to draw the NFA, here I left the graph and the regular expresion:
In the graph, the states are the number in base 10 mod 3.
For example: to go to state 1 you have to have 1, then you can add 1 or 0, if you add 1, you would have 11(3 in base 10), and this number mod 3 is 0 then you draw the arc to the state 0.
((0*)((11)*)((1((00) *)1) *)(101 *(0|((00) *1 *) *0)1) *(1(000)+1*01)*) *
And the other regex works, but this is shorter.
Thanks a lot :)
I know this is an old question, but an efficient answer is yet to be given and this question pops up first for "binary divisible by 3 regex" on Google.
Based on the DFA proposed by the author, a ridiculously short regex can be generated by simplifying the routes a binary string can take through the DFA.
The simplest one, using only state A, is:
0*
Including state B:
0*(11)*0*
Including state C:
0*(1(01*0)*1)*0*
And include the fact that after going back to state A, the whole process can be started again.
0*((1(01*0)*1)*0*)*
Using some basic regex rules, this simplifies to
(1(01*0)*1|0)*
Have a nice day.
If I may plug my solution for this code golf question! It's a piece of JavaScript that generates regexes (probably inefficiently, but does the job) for divisibility for each base.
This is what it generates for divisibility by 3 in base 2:
/^((((0+)?1)(10*1)*0)(0(10*1)*0|1)*(0(10*1)*(1(0+)?))|(((0+)?1)(10*1)*(1(0+)?)|(0(0+)?)))$/
Edit: comparing to Asmor's, probably very inefficient :)
Edit 2: Also, this is a duplicate of this question.
For some who is learning and searching how to do this:
see this video:
https://www.youtube.com/watch?v=SmT1DXLl3f4&t=138s
write state quations and solve them with Axden's Theorem
The way I did is visible in the image-result is the same as pointed out by user #Kert Ojasoo. I hope i did it corretly because i spent 2 days to solve it...
n+2n = 3n. Thus, 2 adjacent bits set to 1 denote a multiple of 3. If there are an odd number of adjacent 1s, that would not be 3.
So I'd propose this regex:
(0*(11)?)+

Generating letters of the english alphabet

I have homework due that states that I need to write a program that generates the first 15 letters of the english alphabet. I can't delcare and set 15 different variables or constants. The letters must be displayed in a number of columns initially set by the user. the numbers have to be aligned in columns. Can anyone help? Maximum number of columns is 7 and the minimum is 1.
Here's some pseudo-code to get you started. Read it, understand it, then try to implement it.
get numcols from user
if numcols < 1 or numcols > 7:
print error and exit
ch = 'a'
for count = 1 to 15:
output ch followed by space
add 1 to ch
if count is an integral multiplier of numcols:
output newline
endif
endfor
if numcols is not equal to 3 or 5:
output newline
endif
It's pitched at about the level of your homework (no fancy stuff and the smallest hint of awkwardness) and should map reasonably well into C code.
As part of this implementation, you should research:
the fact that character constants like 'a' are really integers in disguise.
remainder or modulus (%) operators and how/why they are useful here.
getting user input with scanf.
putchar for outputting characters.
why you have that final if statement :-)
Here is a hint:
ASCII code of A is 65, B is 66, C is 67 and so on. You can do it in a loop starting from 65 and going on for 15 iterations.
This can be done with two nested loops, one for the vertical and one for the horizontal. since the numbers are in sequence in value you can increment the variable for the character each time.
I don't want to give away more than that unless another user says I should. I've already given a lot of help and I'm sure you can figure out the rest.
If you feel you need more help I'll try to not give too much but explain more.

Regex check if number to n-root is not floating point

I am trying to work out how to do something using regular expressions.
Basically, I want to check if a number is equal to a base number (i.e. 2) to the power of n.
For example, I need something thats checks if number i == 2, 4, 8, 16 or 32 then do something.
Edit:
The problem lies where the number is actually coming from a varchar column in a legacy database. I could parse it out then do something like kobi reccommended but there is another problem where the number is in a delimited list i.e. (1,2,3,32). Therefore, I thought it would be easier to use regex as it would save a number of steps.
Thanks in advance.
In Python:
import re
a = str(bin(number))
if re.match(r"[^1]*1[^1]*$", a):
print "power of two"

How to validate with regex that a string is OK as long as it contains 10 digits?

I'm processing input from a Web form. Basically, all I care about is that the value provided includes 10 digits, no more, no less.
These would be valid inputs:
1234567890
123 456 789 0 Hello!
My number is: 123456-7890 thanks
These would be invalid inputs:
123456789033 (too long)
123 Hello! (too short)
My number is one five zero nine thanks (no digits)
I've tried many different things with Regextester but it never matches correctly. I'm using the 'preg' setting (which is what I figured my CMS Typo3 uses) and my closest attempt is:
([0-9][^0-9]*){10}
which is kinda lame but is the closest I got.
Cheers!
EDIT: I cannot use any programming language to implement this. Imagine that I have a admin console field in front of me, in which I must enter a regular expression that will be used to validate the value. That's all the latitude I have. Cheers.
I think you've got the right idea. Maybe you can simplify it as (\d\D*){10}
If the regex has to match the complete string, you would want \D*(\d\D*){10}
UPDATE: It looks like you need ^\D*(\d\D*){10}$ to make sure you match the complete string.
A regular expression is not always the best tool for this kind of job. In this case it's probably easier and simpler to write a function to count the number of digits in a string. (Since you didn't mention a programming language, I'll use Python in my example.)
def count_digits(s):
return len([x for x in s if x.isdigit()])
Then, you can use it like this:
s = "My number is: 123456-7890 thanks"
if count_digits(s) == 10:
print("looks okay")
else:
print("doesn't contain 10 digits")