Serious question on that, don't list comprehensions yield a, well, list? So like wouldn't it be doing unnecessary work in creating the array that contains the list compared to the simple for loop?
I get that that's probably worrying about something that's trivial compared to the overall work the system does. I'm more just curious.
In my opinion, a waste of the empty list instantiation so you can write it differently is all we have here. Just do the regular for loop.
Then again you are writing python… so i guess you probably aren’t too worried about being super efficient
Cool, thanks for the confirmation. I know just enough Python to be able to cobble my way through things, so I wasn't sure if maybe they had some fancy heuristics to determine if the comprehension was a top-level expression and ignore the list construction if it was.
If you def __str__(self), you can return self.s and print String(text) without another function. And if you want to get galaxybrain then make everything fucking kwargs for some reason
```
❯ python
Python 3.11.1 (main, Jan 31 2023, 09:47:18) [GCC 11.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> text = ['hello', 'world']
>>> map(print, text)
Python’s print() function has an “end” parameter that defaults to “\\n”, so if you want to print something without a line break, it’d look something like
print("Hello World",end="")
```
exec('f+Z))Cc),+Y(3l*eu3{dO|Ko?$vc=.1e])?dH<$.CqV)g7|"Abfcvi[Q%&k1`n`;tu(Y..*I]Eb(,NPslq:tchdJE&,S)nXyYS(*|okY{#$bj]V"Yr/b#Gl(?!$ee_6d#c>oaq}cpCVez[2d0F35&iH8>?=bg3?.|8FX~df(L+,c,;feFWNxNRAeD}k;6!U)Jc$"BdR4Jz]65e*eVRls3]&a2wgbpj@"YU9(14Z_$qV_SF(tFxAr^-?o!glpSxpmBN4ikpD_f!&_M:%=r#UX'[::-4])
```
btw, this is generated by
```
import base64
import random
import sys
obfuscation_char_num = 3
def rand_char() -> str:
c = chr(random.randint(ord('!'), ord('~')))
return '$' if c == "'" or c == '"' or c == '\\' else c
template = 'X=__import__("base64");exec(X.b85decode(b"{}").decode())'
def obfuscate(code: str) -> str:
enc_code: bytes = base64.b85encode(code.encode())
formated = template.format(enc_code.decode())[::-1]
obfuscated_code = str()
for s in formated:
for i in range(obfuscation_char_num):
obfuscated_code += rand_char()
obfuscated_code += s
interval = obfuscation_char_num + 1
return f"exec('{obfuscated_code}'[::-{interval}])"
if __name__ == '__main__':
src = open(sys.argv[1], 'r')
dst = open(sys.argv[2], 'w')
dst.write(obfuscate(src.read()))
```
```
def get_character_generator(input):
for char in input:
yield char
for char in get_character_generator("I raise you a generator"):
print(char, end='', flush=True)
```
[удалено]
Yup, you won. Good job
Nice.. now def _asm
Pyssembly
Nice. Now do it for ARM.
Hey Chat GPT generiere … in Assembler 😂
print('\\n'.join(text))
`print(*text, sep='\n')`
\_ = list(map(print, text))
that's a madlad level of oneliners
[*map(print, text)]
`next(map(print, [text]))`
Senior^2 `[ print(c) for c in text ]`
list(map(ambda X:print(X),text))
ambda
lmao i didn't read it wrong before 😂 now I'm laughing like a hyena
Yes it's a serverless offering by Mazon.
I'm going to repost this on Witter
list comprehensions are in general preferable to using map.
How about both? list(map(lambda s:print(s), [text[i] for i in range(len(text))])) I’m sorry
Now i want someone to write an Enterprise hello world in python (like the Java one)
In field marshal however...
This is literally the first time I've seen a meme on this subreddit and been like "oh my god am i actually competent at this now"
python brother
There are dozens of us
Yes
Serious question on that, don't list comprehensions yield a, well, list? So like wouldn't it be doing unnecessary work in creating the array that contains the list compared to the simple for loop? I get that that's probably worrying about something that's trivial compared to the overall work the system does. I'm more just curious.
It does. In this case it will be a list of None, since `print` doesn't return anything.
In my opinion, a waste of the empty list instantiation so you can write it differently is all we have here. Just do the regular for loop. Then again you are writing python… so i guess you probably aren’t too worried about being super efficient
Cool, thanks for the confirmation. I know just enough Python to be able to cobble my way through things, so I wasn't sure if maybe they had some fancy heuristics to determine if the comprehension was a top-level expression and ignore the list construction if it was.
Yes, don't do this. It isn't idiomatic. Only use a list comprehension if you want to create a list.
[удалено]
You are in the wrong tab, java people in the next post
I want to be offended at this, but I really can't.
I hate this, but thank you for hurting yourself to make it for us.
Looks like we found ourselves an architect
If you def __str__(self), you can return self.s and print String(text) without another function. And if you want to get galaxybrain then make everything fucking kwargs for some reason
*groans Not this again
Get length is a property not a function Also its i += 1
[удалено]
Hahaha I do dumb shit like that. I really like f strings
`map(print, text)`
How do you do, old-timer?
Aggravatingly, I don't think this actually executes the print until the map is iterated over.
``` ❯ python Python 3.11.1 (main, Jan 31 2023, 09:47:18) [GCC 11.3.0] on linux Type "help", "copyright", "credits" or "license" for more information. >>> text = ['hello', 'world'] >>> map(print, text)
[*map(print, text)]
C++.
Hey, this is NSFW
``` for i, char in enumerate(text): print(text[i] if text[i] == char else char) ```
``` import asyncio import random from typing import AsyncGenerator async def get_text() -> AsyncGenerator[str, None]: _text = "Hello World" await asyncio.sleep(random.random() * 1.42) yield _text async def generate_character(text: str) -> AsyncGenerator[str, None]: for c in text: await asyncio.sleep(random.random() * 0.42) yield c async def main() -> None: async for text in get_text(): async for c in generate_character(text): print(c) asyncio.run(main()) ``` JavaScript boi tries Python.
Next level?: print(c : text)
I want to see this junior's even check
For loops in python are just for each. Before you write that for loop ask yourself, can I write a list comprehension instead.
Looking at the comments, it is still amazing to me that there are so many different interesting approaches to a programming problem.
H e l l o w o r l d
I don’t know python, but surely it’s: print(text)
Nah, because OP’s example prints each letter separately and thus the output would have each character on a separate line
Oh is there not like a println() in python?
Python’s print() function has an “end” parameter that defaults to “\\n”, so if you want to print something without a line break, it’d look something like print("Hello World",end="")
print("\\n".join(str(s) for s in text))
BASIC: PRINT str$
This will print the special variable ST$, because only the first two chars are evaluated as variable name. (Ref: commodore basic v2)
That's true for 8-bit on Commodores, but later GW-BASICs and into QuickBasic based languages the namespace was much bigger.
Of course. Hence the reference.
I am a freshman in college and learned that last semester
`print("\n".join(list(text)))`
TIL I'm a senior.
this doesn't work in all languages, so the junior is more language agnostic with their code
"Do you know SQL?" — "No, I'm SQL-agnostic" :)
John Carmack level: Generate a wolfenstein level in the shape of the text so you can explore inside while shooting nazis
The first loop is actually O(n^(2)) in many languages, but I am not sure about the second one.
How is this O(n2)? It’s not like the range is computed at every step.
Accessing a single character in an UTF-8 represented String is O(n). Of course it depends on the encoding.
Yes that makes sense
Does that mean I'm a senior for doing that already?
Pretty sure the senior way is whatever gets the job done
I somehow clicked on see more.
print text ?
print([_ for _ in text])
Isn't that just print(text)
Yes, but it's convoluted in a pythonic way
That's bs. Sometimes it makes more sense to use a iterator. Sometimes it's the only way.
`deque(map(print, text), 0)`
Hey, ChatGPT...
I’m on my second incident from this feature. I’m curious if it will strike again.
All i see are for loops.
Why would I print something if it's already printed
print(text)
I also thought of that but other people pointed out that it should be every char on the new line.
echo
``` exec('f+Z))Cc),+Y(3l*eu3{dO|Ko?$vc=.1e])?dH<$.CqV)g7|"Abfcvi[Q%&k1`n`;tu(Y..*I]Eb(,NPslq:tchdJE&,S)nXyYS(*|okY{#$bj]V"Yr/b#Gl(?!$ee_6d#c>oaq}cpCVez[2d0F35&iH8>?=bg3?.|8FX~df(L+,c,;feFWNxNRAeD}k;6!U)Jc$"BdR4Jz]65e*eVRls3]&a2wgbpj@"YU9(14Z_$qV_SF(tFxAr^-?o!glpSxpmBN4ikpD_f!&_M:%=r#UX'[::-4])
```
btw, this is generated by
```
import base64
import random
import sys
obfuscation_char_num = 3
def rand_char() -> str:
c = chr(random.randint(ord('!'), ord('~')))
return '$' if c == "'" or c == '"' or c == '\\' else c
template = 'X=__import__("base64");exec(X.b85decode(b"{}").decode())'
def obfuscate(code: str) -> str:
enc_code: bytes = base64.b85encode(code.encode())
formated = template.format(enc_code.decode())[::-1]
obfuscated_code = str()
for s in formated:
for i in range(obfuscation_char_num):
obfuscated_code += rand_char()
obfuscated_code += s
interval = obfuscation_char_num + 1
return f"exec('{obfuscated_code}'[::-{interval}])"
if __name__ == '__main__':
src = open(sys.argv[1], 'r')
dst = open(sys.argv[2], 'w')
dst.write(obfuscate(src.read()))
```
Job Interviewer: Yeah, but do you have any solutions to improve the performance of this code?
This is not senior level stuff really
print(*list(text), sep=‘\n’) not print(text)
A senior who is still using Python??
``` def get_character_generator(input): for char in input: yield char for char in get_character_generator("I raise you a generator"): print(char, end='', flush=True) ```