String sorting in python - python

Beginner here.
I'm creating quiz project and I have string :
s = "1. You are _____ me.
A) older B) oldest C) older than D) older then
2. New York is _____ Paris.
A) dirty B) dirtier than C) the dirtiest D) dirtier
3. Prague is one of the _____ cities in Europe,
A) most beautiful B) more beautiful
C) beautiful D) the most beautiful
4. How many children _____ they _____?
A) have / got B) have / get
C) does / got D) has / got"
I need to slice it test numbers. result must look like this:
result = {
"test_number": test number,
"question": question text,
"options": [test options],
}

Related

Maximize the spread of all nurses in Scheduling

model.Maximize(
sum(shift_requests[n][d] * shifts[( n, d)] for n in all_nurses
for d in all_days))
Curious how I could change the above (which is optimizing for shift requests) to the below, which would optimize for the spread. I'm trying to actually spread out the assignments as much as possible. Thoughts?
model.Maximize(
np.std(shifts[( n, d)] for n in all_nurses
for d in all_days))
You can try the following
You have a list of Booleans: b(i) means mean span between 2 working days is greater or equal than i
you need to ensure consistency between b(i) variables: b(i) => b(i-1)
if nurse n is working on day d, and b(i) is true, then work[n, d + i - 1] is false. Encoded as model.AddBoolOr(work[n, d].Not(), b(i).Not(), work[n, d + i - 1].Not()] for all the relevant n, d, i.
maximize i where b(i) is true. A crude solution would be just model.Maximize(sum(b(i))). Maybe this can be improved.

Count the moves of hanoi tower

I am trying to count the moves of hanoi tower
In [4]: %paste
count = 0
def hanoi(n, a, b, c):
global count
if n == 1:
count += 1
else:
hanoi(n - 1, a, c, b)
hanoi(1, a, b, c)
hanoi(n - 1, b, a, c)
hanoi(10, "A", "B", "C")
## -- End pasted text --
In [5]: print(count)
1023
The above solution employed the global keyword,
How could get it done if not introducing global?
With a bit of refactoring to make the function recursive with a common count variable:
def hanoi(n, a, b, c):
count = 0
if n == 1:
count += 1
else:
count += hanoi(n - 1, a, c, b)
count += hanoi(1, a, b, c)
count += hanoi(n - 1, b, a, c)
return count
Output
>>> hanoi(10, "A", "B", "C")
1023
Or even without any count variable:
def hanoi(n, a, b, c):
if n != 1:
return hanoi(n - 1, a, c, b) + hanoi(1, a, b, c) + hanoi(n - 1, b, a, c)
return 1
print (hanoi(10, "A", "B", "C"))
Output
1023
There is no need to use complex codes for that. After all, that is math based, so just use this simple one line solution, that works every time in every code.
Place it in the end of hanoi code using tab ofc... but before
hanoi(10, "A", "B", "C")
and remove all that "count" staff that was never even needed.
print(2**n-1)
Lets say, your hanoi code ends like this, with this math code:
def hanoi(n,f,h,t,):
if n==0:
pass
else:
hanoi(n-1,f,t,h)
move(f,t)
hanoi(n-1,h,f,t)
print(2**n-1)
hanoi(4,"A","B","C")
And you do not have to make an other code to count that. Just chance the number at the and of code. Less is better and faster...
If you make all codes long and spread, it will slow down the results. PC:s are made to do math, so why should we not use that!?
The other thing is that, hanoi code itself is easier to make than these count codes. Lets compare with this randint code...
Hanoi code needs just one print/format on it like this:
print("Move object from {} to {}!" .format(f,t))
You can also get rid of int and str in code. Any option, you can cut out of code, makes its faster and cleaner. When there is millions of codes, this comes reasonable to do. Unless you want to hide something in/out of master code.
Reason is, there is only one starting/end point. Empty points (when ever they are empty) are only "help" points. I'm not going to go too deep to that.
But in the end...
Combinatorics in math or even in Python, gives your starting/end points and numbers of events in how many(n), where(A,B,C...) and some times when and even timers/triggers(there is many options)
Learning Hanoi Tower is useful and powerful, to understand, so when your make your code, your can use these given options and print them, with simple math code instead, of doing complex codes with errors. If you like, you can ad there as many tiny hanoi codes as you like and still, you can get right answer(just separate them with r or n option to row or new line).
I hope this was better answer now, after explanation of combinatorics.

Tower of Hanoi, problem with understanding the code

I have a problem with understanding this code.
code:
def hanoi(n, A, B, C) :
' ' ' n - number of discs
A - initial stick
B - Middle stick
C - Goal stick ' ' '
if n > 0:
hanoi(n - 1, A, C, B)
print('Disc', n, 'from', A, 'to', C)
hanoi(n - 1, B, A, C)
hanoi(3, 'A', 'B', 'C')
When I use debugger I see in if loop, the control flow first checks the if condition, and after that it goes down in hanoi(n - 1, A, C, B), and when it executes that for the first time, It does it again three more times, instead to going on print('disc', n, 'from', A, 'on', C). I can't figure it out why it is doing that, and what kind of rules it uses for doing this. I working in IDLE environment.
Here is results of compiled program:
Disc 1 from A on C
Disc 2 from A to B
Disc 1 from C to B
Disc 3 form A to C
Disc 1 from B to A
Disc 2 from B to C
Disc 1 from A to C
It's because
hanoi(n - 1, A, C, B)
#is before
print(...)
So it will destack before printing every subchild
Switch those two lines like
print('Disc', n, 'from', A, 'to', C)
hanoi(n - 1, A, C, B)
hanoi(n - 1, B, A, C)
And the code works fine :)
You must understand the implications of the code flow going down a branch such as hanoi(n - 1, A, C, B) . That statement is before the print statement, and makes a recursive call to the function in a lower stack. The lower stack must be evaluated completely before you can proceed further with the print statement.
The issue here is that you may find it hard to visualize "lower stacks" and recursion in a simple IDE. Here is a pythontutor visualization for the same. link
Bottom line though: The print statement did not execute simply because the control flow/code flow never encountered the print statement, and branched off to a lower stack because of the recursive call. The print happens exactly when it's supposed to, because that's how the code is written.

Translating normal code to 3AC - Three address code

I am trying to make an algorithm that will transcribe certain input, such as:
(a * b) / (c * d)
and will print out input like this in 3AC:
t1: a * b
t2: c * d
t3= t1/t2
Has anyone any suggestions?
Algorithm for transcribing such equations is pretty straightforward.
Obviously first task would be to convert given equation to reverse polish notation. From here you already have perfectly defined order of execution.
You now have to prepare stack for operands. At this point you just do it like normal RPN, except instead of performing operation and putting result back on operands stack, you have to print new TAC instruction with new number, and put that symbol back on stack instead of result.
In your example RPN will be : a b * c d * /
So a and b go on the stack. When we encounter * we pop 2 items from stack, print t1 := a * b and put t1 on stack. Now we traverse RPN more and put c and d on stack. Now we come to encounter another *, so again lets pop 2 items from stack, print them with new TAC symbol t2 := c * d and put our new symbol t2 back on stack.
lastly we encounter / so again we pop 2 items from stack, create new symbol and print it :) t3 := t1 / t2.
This is much simpler than standard RPN calculator.
One way you could do this is to first turn this into an AST
then traverse the AST by recursive function to make your code.
for your example your code will be turned into something like this
("/", ("*", "a", "b"), ("*", "c", "d")
traverse with said function and turn it into this
t0 = a * b
t1 = c * d
t2 = t0 / t1

Python - VERY basic 'encryptor'

Me and a guy at work decided to try and make a basic python program that would 1) jumble a string & 2) un-jumble the string. The idea was that we can send each other absolute rubbish.
My first attempt (I am awful at this):
x = ("This is the text")
x1 = x.replace("a","r")
x2 = x1.replace("b","w")
x3 = x2.replace("c","z") #Do this for the whole alphabet
print(x3) #both upper and lower case
Then do the same but back to front to unscramble.... It works but its also longggggg....
Code from this article: http://gomputor.wordpress.com/2008/09/27/search-replace-multiple-words-or-characters-with-python/ suggests creating a method as follows:
def replace_all(text,dic):
for i,j in dic.iteritems():
text = text.replace(i,j)
return text
reps = {'a':'r','b':'w','c':'z'} #for the whole alphabet again (yawn)
x = ("This is the text")
txt = replace_all(x, reps)
print(txt)
Will this work? I have seen iteritems() get bad press elsewhere??
iteritems returns a list of key, value pairs from the dictionary. The only reason to watch out for iteritems is that it was removed in python 3.0 because dict.items now does the same thing.
As for the code, its functionally correct (except that x should be my_text), though the encryption strength isn't very high.
There are many algorithms that use simpler encryption key methods (your reps dictionary) and produce higher quality encryption. If you are working in python why not use a simple library like https://pypi.python.org/pypi/simple-crypt to get a higher quality encryption/decryption?
If you're working in Python 2 rot13 is a built in codec:
>>> 'This is the text'.encode('rot13')
'Guvf vf gur grkg'
>>> 'Guvf vf gur grkg'.decode('rot13')
u'This is the text'
http://docs.python.org/2/library/codecs.html
Drum roll please.....
>>> msg="The quick brown fox jumps over the lazy dog"
>>> cyphertext = ' '.join(w[1:]+'-'+w[0]+'ey' for w in msg.split())
>>> cyphertext
'he-Tey uick-qey rown-bey ox-fey umps-jey ver-oey he-tey azy-ley og-dey'
>>> ' '.join(w[-3] + w[:-4] for w in cyphertext.split())
'The quick brown fox jumps over the lazy dog'
Note that the non-standard treatment of "the" and "quick" along with the possible confusion as to the "ey" vs "ay" suffix enhances security.
why not use string.translate
import string
tab = string.maketrans(string.ascii_letters,string.ascii_letters[13:]+string.ascii_letters[:13])
message = "hello world!"
encoded = message.translate(tab)
tab_decode = string.maketrans(string.ascii_letters[13:]+string.ascii_letters[:13],string.ascii_letters)
print encoded.translate(tab_decode)
with string.translate you can define any mapping you want
key = "lkj234oiu1563lop|.)(*&^%$##!" #note key must be as long as your alphabet(in this case 52)
mapping = (string.ascii_letters,key)
tab_encode = string.maketrans(*mapping)
encoded = "hello world".translate(tab_encode)
tab_decode = string.maketrans(*list(reversed(mapping)))
decoded = encoded.translate(tab_decode)
another thing you could do is base64 encoding (note this is not encryption, however it is probably about as hard to crack as a simple ceasar cipher)
import base64
decoded = "Hello World!!!"
encoded = base64.b64encode(decoded)
print encoded
print base64.b64decode(encoded)
Here is a complete solution that you can use to learn from. You can use it to both encode and decode you messages. Just specify what you want to do and then type in your message. A blank line indicates that you are finished with your message and signals for the requested transformation to take place.
import string
PLAIN_TEXT = string.ascii_letters
CIPHER_TEXT = 'kWFUsDYeAxolSLTwChgNJtaMvQIzRZVrPEyfiKXGcdBunbqHjpOm'
CIPHER_TABLE = str.maketrans(PLAIN_TEXT, CIPHER_TEXT)
PLAIN_TABLE = str.maketrans(CIPHER_TEXT, PLAIN_TEXT)
def main():
while True:
choice = input('Encode or Decode? ').lower()
if choice:
if 'encode'.startswith(choice):
process_text(CIPHER_TABLE)
continue
if 'decode'.startswith(choice):
process_text(PLAIN_TABLE)
continue
print('Please try again.')
def process_text(table):
buffer = []
while True:
line = input('> ')
if not line:
break
buffer.append(line.translate(table))
print('\n'.join(buffer))
if __name__ == '__main__':
main()
If you want an even better solution, bookmark Wabol Talk and use that to create your messages. You can look at its source and see that it is implemented in Python as well. The program encrypts messages and encodes them in the "Wabol" language. I can also provide a GUI version of the program if you want.
Here are some basic, very insecure ciphers with which to have fun! By the way, I was googling around and I found some neat slides from Northeastern University on basic cryptosystems. Also, I apologize if my lettering habits differ from wiki or anyone else; I just go with my habits.
Shift Cipher
The shift cipher has an integer ring (e.g. the letters of the alphabet, A-Z, numbered from 0-25 usually, and we typically call this Z26) and an integer key, let's call it k. In order to build this ring, we will use modular arithmetic, basically meaning our numbers will only ever be between 0 and 25 if our modulus, let's call it n, is 26.) For any given plaintext letter, let's call it m, we can encode it by adding k to m, modulo n - and viola! We have our ciphertext letter, let's call it c.
To put it mathematically: c ≅ m + k (mod n). And logically, m ≅ c - k (mod n). To put it programmatically:
def encrypt(m, k, n):
return (m + k) % n
def decrypt(c, k, n):
return (c - k) % n
Substitution Cipher
Substitution ciphers are much harder to crack than shift ciphers, but they are still easy to crack using many characteristics of the English language. Google them if you like - but I gotta say, a nice program to help you is freq.c.
A substitution cipher consists of the same ring, with a mapping of plaintext characters to ciphertext characters. For instance, A = G, B = Q, C = E, D = T and so on. A dictionary's really great for this: k = {'A' : 'G', 'B' : 'Q', ...}. If you want to decrypt, you can invert the dictionary.
def encrypt(m, k):
return k[m]
def decrypt(c, k):
for key, value in k.items():
if value == c: return key
Affine Cipher
This cipher adds modular multiplication to the shift cipher. The key is now a pair a, b where a must be coprime to n, so that it has a modular multiplicative inverse. To encrypt, multiply m with a (modularly) and then add b (modularly). Choosing an a which is coprime isn't exactly obvious - basically, you need to make sure that the greatest common divisor of a and n is 1.
Mathematically: c ≅ (am + b) mod n and therefore m ≅ a^-1(c - b) mod n. Programmatically:
def encrypt(m, a, b, n):
return (a*m + b) % 25
And I'm not going to write a clever solution for finding this modular multiplicative inverse... you can use the extended Euclidean algorithm for this, but for me it's just easier to bruteforce it when n is so small.
def getModularMultiplicativeInverse(a, b, n):
for x in range(2, n):
if (a * x) % n == 1:
return x
def decrypt(c, a, b, n):
return (getModularMultiplicativeInverse(a, b, n) * (c - b)) % n
These were just some little examples... less for the code, more for the chatter.

Categories