RandomBool=lambda:bool(ord(open('/dev/urandom','rb').read(1))&1) # This is also terribad, and the previous randint ones are better, but why not keep having fun
RandomBool=lambda:id(object())&1==0- RandomBool=lambda:bool(ord(open('/dev/urandom','rb').read(1))&1)
- # This is also terribad, and the previous randint ones are better, but why not keep having fun
import codewars_test as test from solution import RandomBool @test.describe("Example") def test_group(): @test.it("test case") def test_case(): test.assert_equals(RandomBool() in [True, False], True) @test.it("less easily fooled test case") def test_case(): count = 10000 margin = count // 10 # 10 percent off is reasonable check = {True: 0, False: 0} for _ in range(count): check[RandomBool()] += 1 print(check) test.expect(abs(check[True] - check[False]) < margin)
- import codewars_test as test
- from solution import RandomBool
- @test.describe("Example")
- def test_group():
- @test.it("test case")
- def test_case():
- test.assert_equals(RandomBool() in [True, False], True)
- @test.it("less easily fooled test case")
- def test_case():
- count = 10000
- margin = count // 10 # 10 percent off is reasonable
- check = {True: 0, False: 0}
- for _ in range(count):
- check[RandomBool()] += 1
- print(check)
- test.expect(abs(check[True] - check[False]) < margin)
Fundamentals
def main(): play_again = True while play_again: word = input('Enter a string\n> ') print(f'>> The output is: {word.upper()}') while (play_again := input('Would you like to play again? (y)es or (n)o :\n> ')): play_again = play_again.lower() if play_again == 'n': print('goodbye') play_again = False elif play_again != 'y': print('Incorrect input!') continue break if __name__ == '__main__': main()
- def main():
word = input('Enter a string> ')print(f'>> The output is: {word.upper()}')- play_again = True
- while play_again:
- word = input('Enter a string
- > ')
- print(f'>> The output is: {word.upper()}')
- while (play_again := input('Would you like to play again? (y)es or (n)o :\n> ')):
- play_again = play_again.lower()
while True:play_again = input('Would you like to play (y)es or (n)o :\n> ')if play_again == 'y':return main()elif play_again == 'n':print('goodbye')- if play_again == 'n':
- print('goodbye')
- play_again = False
- elif play_again != 'y':
- print('Incorrect input!')
- continue
- break
else:print('Incorrect input!')continue- if __name__ == '__main__':
- main()
import sys from pathlib import Path # looks like the original tries to print the contents of # all files in the top 3 levels of the virtual environment # but has a problem where it's iterating over a string instead # of a recursive directory listing # namely it's like # >> os.listdir('.venv') # ['.bar', 'baz', '.foo'] # then iterating over each one as if it's a dirname # so it tries opening .venv/., then .venv/b, then .venv/a then .venv/r # it's also printing a listdir of the nested directories instead of saving # the listdir, which might make it work if subsequently iterated # I'd imagine it's trying to print all text files in the virtual environment # this would do what the original looks like it's trying to do, # with a bonus of actually working if the venv isn't in ${pwd}/.venv # and if you're in a venv def walk_dont_run(): if sys.prefix != sys.base_prefix: start = Path(sys.prefix) if start: for path in start.glob('**/*'): try: print(path.read_text()) except: pass
import osimport subprocessprint((ve := os.listdir(".venv")))for x in ve:try:print(x, os.listdir(f".venv/{x}"))for y in x:try:print(y, os.listdir(f".venv/{x}/{y}"))for z in y:try:with open(f".venv/{x}/{y}/{z}","r") as file: print(file.open())except:print("...goes on...")except:- import sys
- from pathlib import Path
- # looks like the original tries to print the contents of
- # all files in the top 3 levels of the virtual environment
- # but has a problem where it's iterating over a string instead
- # of a recursive directory listing
- # namely it's like
- # >> os.listdir('.venv')
- # ['.bar', 'baz', '.foo']
- # then iterating over each one as if it's a dirname
- # so it tries opening .venv/., then .venv/b, then .venv/a then .venv/r
- # it's also printing a listdir of the nested directories instead of saving
- # the listdir, which might make it work if subsequently iterated
- # I'd imagine it's trying to print all text files in the virtual environment
- # this would do what the original looks like it's trying to do,
- # with a bonus of actually working if the venv isn't in ${pwd}/.venv
- # and if you're in a venv
- def walk_dont_run():
- if sys.prefix != sys.base_prefix:
- start = Path(sys.prefix)
- if start:
- for path in start.glob('**/*'):
- try:
with open(rf".venv/{x}") as f:print(f.read())except:print()except:with open(rf".venv/{x}") as f:print(f.read())- print(path.read_text())
- except: pass
import codewars_test as test from solution import walk_dont_run @test.it("test case") def test_case(): test.assert_equals(1+1, 2)
- import codewars_test as test
- from solution import walk_dont_run
- @test.it("test case")
- def test_case():
- test.assert_equals(1+1, 2)
from random import choice class Random_Bool: @staticmethod def r(): return choice((True, False))
from random import getrandbitsclass Random_Bool(object):def __repr__(self):return repr(self.r())def __init__(self):self.r = lambda: bool(getrandbits(1))random_boolean = Random_Bool()- from random import choice
- class Random_Bool:
- @staticmethod
- def r():
- return choice((True, False))