Easiest way to assign a number to the alphabet? [duplicate] - python

This question already has answers here:
confusion with encryption
(3 answers)
Closed 3 years ago.
I need to find the best way to assign each letter of the alphabet a random number 1-26 (using each number once). Or assign each number 1-26 a random letter of the alphabet (using each letter once).
For example:
a = 6, b = 12, c = 91
or
1 = g, 2 = a, 3 = k
I've tried assigning randint to each letter and repeating if the number has been used already but its really long.

Why not something like this?
import random
x=['A','B','C'.....]
y=list(range(1,27))
random.shuffle(y)
combo=list(zip(x,y))
you could of course make the final output a dict or something

Related

How can i offset a string in python? [duplicate]

This question already has answers here:
Python - Caesar Cipher
(3 answers)
Closed 2 years ago.
This is a codecademy challenge, i am trying to offset the letters in the input string in relation to the english alphabet. lets say if we enter d with an offset of 3 it should return a. What am i doing wrong ?
I am new to python i started 3 days ago because of the quarantine, love it so far.
import string
a = list(string.ascii_lowercase)
word = "assa"
i = ""
j= ""
x = 0
for j in a:
for i in word:
if i == j:
x = (int(a.index(j)) - 10)
z = str(a[x])
sol = word.replace(i,z)
print(sol)
#def cipher(word):
sol = word.replace(i,z)
Every time, this goes back to the original value of word, figures out the result of making the replacement, and gives the name sol to the result. For your general approach to work, you would need to rename the result as word.
When you do this, you should not loop over the characters in word. .replace(i, z) already replaces every appearance of i with z (since i == j, you could equivalently have used j). It is also a bad idea to modify the thing you are looping over.
However, there is a much most straightforward way to implement this entire solution, using str.translate and str.maketrans - see for example.

Comparing two lists (check how many times short list occurs in long list) [duplicate]

This question already has answers here:
Best way to determine if a sequence is in another sequence?
(10 answers)
Counting sublists in a list
(2 answers)
Closed 4 years ago.
I am doing homework and I got stuck with my code.
The question is about two lists containing string letters in it as elements and we want to know how many times the small text occurs in the long one.(with order)
Example:
long=['a','k','g','j','a','k','k','a','k','g']
small=['a','k','g']
then the answer should be 2 as it occurs twice(first and in the last part)
As I tried:
def search(long,small):
words=[]
for k in range(len(long)):
for l in range(len(small)):
if long[k]==small[l]:
words.append()
return(words)
First tried to write down the matches in the list and then divide the len of list to get the number. But unfortunatly this gives me the first match and i am not sure how to get all the matches. Hope you guys can help me.
You can do it by advancing through elements of small and resetting the counter if you encounter an "odd" element:
def search(long,small):
indexCt = 0
count = 0
for k in range(len(long)):
if long[k]==small[indexCt]:
indexCt += 1
else:
indexCt = 0
if(indexCt == len(small)):
indexCt = 0
count += 1
return count
Slice the long list into pieces len(small) long and check for equality each time
def search(long, small):
return sum(long[i:i+len(small)]==small for i in range(len(long)-len(small)+1))
print(search(['a','k','g','j','a','k','k','a','k','g'], ['a','k','g']))
Output:
2

Generate random sequence of letters of length K from list of letters [duplicate]

This question already has answers here:
Generate a random letter in Python
(23 answers)
Generate 'n' unique random numbers within a range [duplicate]
(4 answers)
Closed 4 years ago.
I am trying to write a function that generates a random sequence of letters (letters from a list), and the sequence length is K (K is passed to the function as an argument when function is called). I am stuck, and I don't understand how to generate random sequence of letters.
As mentioned by we can loop the random choice on the given list for k times to get the desired output
Def fn(letters,k):
return [np.random.choice(letters) for _ range(k) ]]

How to check for consecutive repeating characters in a string [duplicate]

This question already has answers here:
Python: A program to find the LENGTH of the longest run in a given list?
(6 answers)
Closed 4 years ago.
I'm trying to figure out how to check if certain characters are repeated after each other in a single string, and if so, how often are they repeated.
Example:
str.x = 'abbbjjaaaal'
As the return I need the integer 4, as in this case the longest consecutive repetition of a single character in the string x is a, and it is repeated 4 times.
some_str = 'abbbjjaaaal'
groups = [(k , len(list(g))) for k, g in groupby(a, str)]
groups.sort(key=lambda k:k[1], reverse=True)
print(grups[0][0])

How do I convert a set to an interger in python? [duplicate]

This question already has answers here:
Convert list of ints to one number?
(19 answers)
Closed 5 years ago.
I want to generate a random 4 digit number in which none of the digits are repeated.
import random
sets = random.sample(range(0,9), 4)
This generates a random set of 4 digits but I want this as an integer. how do I do that?
(Assuming OP meant all the digits)
Instead of using numbers and have to manipulate to str and back to int, just start with ascii digits:
>>> import string
>>> ''.join(random.sample(string.digits, 4))
'4561'
You can convert to int() if necessary.
It's unclear what the OP intends to do if the first digit is 0.
For a purely numerical approach you can use functools.reduce:
>>> import functools as ft
>>> ft.reduce(lambda s, d: 10*s + d, random.sample(range(10), 4))
2945
You can do this by converting each digit to a string, joining them, and casting them as an integer.
int("".join(map(str,random.sample(range(0,9),4))))
if you need to generate 4 digit number, just for knowledge purpose use.
As suggested by AChampion this solution can contain duplicates
from random import randint
randint(1000, 9999)
Use bernie Solution to generate a random 4 digit number in which none of the digits are repeated.
int("".join(map(str,random.sample(range(0,9),4))))
In case if you want potentially infinite sequence of numbers with 4 unique digits (or any other condition – write your own)
import random
def numbers_gen(left_end, right_end):
while True:
yield random.randint(left_end, right_end)
def are_digits_unique(number):
number_string = str(number)
return list(set(number_string)) == list(number_string)
four_digits_numbers_gen = number_gen(left_end=1000,
right_end=9999)
four_digits_numbers_with_unique_digits_gen = filter(are_digits_unique,
four_digits_numbers_gen)
Works only in Python 3 because filter returns iterator-object (in Python 2.7 it returns list, more at docs)
You can multiply with powers of 10:
sum(10**a*b for a, b in enumerate(reversed(sets)))
This works as long as the first element of sets is not zero.
You could try:
import random
my_set = set()
while len(my_set) < 4:
x = random.choice(range(0,9))
my_set.add(x)
my_num = int("".join(map(str, my_set)))
One line:
int("".join(random.sample("0123456789",4)))

Categories

Resources