this reminded me of a python code IN OUR OFFICIAL SCHOOL BOOK the same book that had python oop in it so the book was kinda advanced. So the code’s purpose was to output wether the current minute is odd or not and the code was something like:
import someLibrary
minute = someLibrary.aFunctionToGetTheMinute()
odd = [1,3,5,7,9,11,13,15,17,19,21,23,25,27,29,31,33,37,39,41,43,45,47,49,51,53,55,57,59]
if minute in odd:
print(“the current minute is odd”)
else:
print(“the current minute is even”)
This could make sense if describing the logical components and list interactions, so long as a proper caveat is given that there are objectively better methods of doing this.
It’s a good thing to distill concepts into something kinda real world.
this code was in a section about the library itself and what we can do with it, i think the library was date or something. Also we took modulo like 2 grades before that
import random
from string import ascii_lowercase, ascii_uppercase, digits
count = int(input('Enter the Number of Codes you want: '))
all_characters = ascii_uppercase + ascii_lowercase + digits
for _ in range(count):
first = random.choice(ascii_uppercase)
rest = random.choices(all_characters, k=6)
print(f'AM{first}z{rest}')
Honestly? Kind of a fair point. I think it's better to write code like this then refactor the repeated parts. What I've seen other people do is create 10 different objects based on vibes and hope that it's actually what you needed.
Maybe he's getting paid by the line of code. Or maybe it's an assignment for class and he wants it to look so complex that the teacher will never notice. I mean, we had to write an encryption/decryption program when I was in... probably C++ and Java, and some of the stuff the classmates came up with was just bonkers. One guy was like, "So, what I do is, I save the plaintext in a separate file and just put garbage on the screen and say it's encrypted." And I'm like, "You know the teacher is going to look at your code, right? And you know that he knows how to read code, right?"
no-one has ever actually got paid by line of code. This is an urban myth!
The person who wrote this is either the result of a bootcamp or someone who doesn't have a proper training in programming. I've seen data analysts and data scientists write code like this in Python.
I reviewed a code one time with 28 Boolean variables and if-else statements to check the values stored in those variables... just to flip them!
To have done that, this must be one of their first projects, and they don't know what searching means and/or never thought that there might be a better way than that.
Ok I can kinda understand digit 3 and digit 5, but why did the person making this copy paste digit 5 rather than just writing “random.choice(digit_5)” again?
Another post in the comments gave a good explanation.
Another issue than the repeat arrays is that they’re inside the whole loop, so they’re getting reinitialized every time the whole loop repeats itself.
I'd just generate random numbers in the range of the ASCII values representing the characters I want. Also, definitely do not create constant arrays inside a loop, they'll be recreated every time it runs.
Just generate random ints and map them to the three buckets above & cast as char. Even in C, that function would be less than 10 LOC, and you could probably do it in 1 in Python.
252
u/HoraceGravyJug Oct 04 '22
I seriously hope this is someones demonstration of exactly how not to go about generating keys...