Transposition Cipher errors python - python

I'm trying to write a function encr(k,m) where the message m is any string over the characters A...Za...z and where the key k is a permutation. When len(m) isn't a multiple of len(k), then I need to append Z's to m to make len(m) a multiple of len(k).
So far I've got
import math, pyperclip
def encr(k, m):
ciphertext = [''] * k
for col in range(len(k)):
pointer = col
while pointer < len(m):
ciphertext[col] += m[pointer]
pointer += 1
return ''.join(ciphertext)
I keep getting an error: TypeError: can't multiply sequence by non-int of type 'list' on line 3, in encr ciphertext = [''] * k
Any help would be great
An example of input would be encr([3, 2, 4, 1, 0], 'SineLaboreNihil')

You current error is that k is a string in your function, and you are attempting to add that to an integer - pointer. I think you meant to add 1 to pointer.
You are also attempting to take range() on that same string. range(len(k)) is what you need to do.
Your new error can be corrected as follows -
ciphertext = [''] * len(k)

Related

Value becoming an int when list is expected in a function

I am doing an exercise to print out all the permutations of [0,1,2,3].
And the output is supposed to be in the same form, like [2,3,1,0] for example.
I wrote down the following code.
def permutations(units):
permutation_list = []
if len(units) == 1:
return units
else:
for i in units:
new = [j for j in units if j != i]
[permutation_list.append([i] + p) for p in permutations(new)]
return permutation_list
print(permutations([0,1,2,3]))
However, it gives me an error saying that p is int and that list [i] and int p can not be added.
units is list and output is a list so I don't understand how p can be int.
Any clarification would be very much appreciated.
Edit. This is the error I get:
[permutation_list.append([i] + p) for p in permutations(new)]
TypeError: can only concatenate list (not "int") to list

Half integer values in Sympy

In the package sympy.physics.quantum.spin there is a function m_values which is called by a lot of the other functions in the package. It takes as input an integer or half-integer, and returns a tuple. Here is the source code, obtained from Github:
def m_values(j):
j = sympify(j)
size = 2*j + 1
if not size.is_Integer or not size > 0:
raise ValueError(
'Only integer or half-integer values allowed for j, got: : %r' % j
)
return size, [j - i for i in range(int(2*j + 1))]
This functions works as I expect it to for integer values. However, if I put in a half-integer value, this function returns an error. Here is an example:
from sympy.physics.quantum.spin import m_values
m_values(0.5) #can also try m_values(3/2)
This code produces the error
ValueError: Only integer or half-integer values allowed for j, got: : 0.500000000000000
So clearly the issue is that size.is_Integer is returning False. How should I input j into m_values so that the function works for half-integers?

I'm getting this error but I tried everything. TypeError: can't multiply sequence by non-int of type 'float'

I'm having an issue with running this simple calculator. Every time I run this program, I get this error:
f = a * l
TypeError: can't multiply sequence by non-int of type 'float'
Does anyone notice my error and how I can correct it?
while True:
print("///Calculator///")
print("======================================")
mq = float(input("Market Quote: "))
if mq==0:
break
a = input("Balance: ")
l = input("Lev: ")
if not l:
l = 500
else:
l = float(l)
0
p = input("Other: ")
if not p:
p = 70
else:
p = float(p)
#-------------------------------------
f = a * l
q = f / l
mr = q * mq
so = 0.40
mm = mr * so
k = p * 10
b = a - mm
x = b / k
h = x / 8
u = round(h, 2)
o = x * mq
j = o / l
c = round(j)
You need to convert all numerical inputs to floats or ints if you want to do numerical calculations on them. You forgot a and p:
a = float(input("Balance: "))
...
p = float(input("Other: "))
Two general tips for the future: give your variables meaningful names (you can skip vowels or whatever for speed, but apart from indexes, one letter variables aren't great) and read error messages! Here 'can't multiply sequence by non-int of type 'float'' means exactly that! The result of any call to input() is a string (a sequence type) and when you multiply a sequence by an integer, Python interprets that as you wanting to repeat it, as in:
>>> 'a' * 4
'aaaa'
But what is it supposed to do with a float? I certainly don't know and neither does it:
>>> 'a' * 4.0
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: can't multiply sequence by non-int of type 'float'
And if the worst comes to the worst and you can't interpret Python's complaints, then you still have the line number - go there and see that all that is being done is: f = a * l so your next step should be to analyse what are a, what are l and why can't Python multiply them!
My point is, yes debugging is hard, but it is a skill, and trying things isn't always the right way to go - sometimes being methodical speeds up the process.

Z3 String/Char xor?

I'm working with Z3 in Python and am trying to figure out how to do String operations. In general, I've played around with z3.String as the object, doing things like str1 + str2 == 'hello world'. However, I have been unable to accomplish the following behavior:
solver.add(str1[1] ^ str1[2] == 12) # -- or --
solver.add(str1[1] ^ str1[2] == str2[1])
So basically add the constraint that character 1 xor character 2 equals 12. My understanding is that the string is defined as a sequence of 8-bit BitVectors under the hood, and BitVectors should be able to be xor'd.
Thanks!
So far I don't expose ways to access characters with a function. You would have to define auxiliary functions and axioms that capture extraction. The operator [] extracts a sub-sequence, which is of length 1 if the index is within bounds.
Here is a way to access the elements:
from z3 import *
nth = Function('nth', StringSort(), IntSort(), BitVecSort(8))
k = Int('k')
str1, str2, s = Strings('str1 str2 s')
s = Solver()
s.add(ForAll([str1, k], Implies(And(0 <= k, k < Length(str1)), Unit(nth(str1, k)) == str1[k])))
s.add( ((nth(str1, 1)) ^ (nth(str2, 2))) == 12)

Type object is not subscriptable while writing algorithm in python

I am trying to convert my algorithm into python code. The algorithm is as follows:
For i = 1 To n
For j = 1 To (m - 1)
del1 = C(i - 1, j) - C(i - 1, j - 1)
del2 = C(i - 1, j + 1) - C(i - 1, j)
If del2 = 0 Then
r = 0
Else
r = del1 / del2
End If
Next i
I tried to convert the above chunk of code step by stop. For del1 I tried to write the python code as follows:
del1 = [[C[i-1,j]-C[i-1,j-1] for j in range(1,(m-1))]for i in range [0,int(n)]]
I get the error TypeError: 'type' object is not subscriptable. Can anyone give me starting point on how to convert the above algorithm into python code ?
Edit:
C = [[0 for j in range(0,int(m))]for i in range(0)]
C = [[1 for i in range(0,int(n))]for j in range(0)]
Thanks.
Jdbaba
should be:
del1 = [[C[i-1,j]-C[i-1,j-1] for j in range(1,(m-1))]for i in range(0,int(n))]
[] -> () on the last range
The above notation will work if C is something like a numpy array that supports multi-dimensional slicing. If C is a list of lists, the following should work:
del1 = [[C[i-1][j]-C[i-1][j-1] for j in range(1,(m-1))]for i in range(0,int(n))]
It looks like you want range(0,int(n)).
In python, anything inside [] ie, square-brackets is called subscriptable -- it represents an array concept.
But, in loops we might use range, where we can have a typo of [] instead of () paranthesis.
The range value is not in a subscript format or separable elements of array.
So, the only solution is to change [] to () and run..

Categories

Resources