kristofer / Is Pythonic?
0 likes
0 forks
1 files
Last active
some examples of good and bad pythonic snippets
Pythonic" code refers to code that is written in a way that is idiomatic to Python, taking advantage of its features and conventions to produce clear, concise, and readable code. Here are some examples showcasing Pythonic principles:
1. List Comprehensions
Not Pythonic:
squares = []
kristofer / dict-count.py
0 likes
0 forks
1 files
Last active
a sample of frequency counting words from a list, in good and bad styles
1 | # You need to write a loop that counts the occurrences of each word in a list. |
2 | # |
3 | # Consider, if you had a big str that was a text, like say: https://zcw.guru/kristofer/hamlet |
4 | # |
5 | # strive to be "pythonic" in your code expressions, okay? |
6 | |
7 | colors = ["red", "green", "blue", "green", "red", "green"] |
8 | |
9 | # Not Pythonic Code |
10 | d = {} |
kristofer / Hamlet by Shake-speare
0 likes
0 forks
1 files
Last active
1 | THE TRAGEDY OF HAMLET, PRINCE OF DENMARK |
2 | |
3 | |
4 | by William Shakespeare |
5 | |
6 | |
7 | |
8 | Dramatis Personae |
9 | |
10 | Claudius, King of Denmark. |
kristofer / Details Matter
0 likes
0 forks
1 files
Last active
a small difference makes results wildly different
1 | |
2 | public class WhoaWhat { |
3 | public static void main(String[] args) { |
4 | System.out.println(doOne() + " " + doTwo()); |
5 | // will print 2048 4 - but why? |
6 | } |
7 | private static int doOne() { |
8 | int result = 2; |
9 | for(int i = 0; i < 10; i++) |
10 | { |
kristofer / BugReporter (java)
0 likes
0 forks
1 files
Last active
cool class from https://github.com/RohitAwate/Everest/blob/master/BugReporter/src/BugReporter.java
1 | /* |
2 | * Copyright 2018 Rohit Awate. |
3 | * |
4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | * you may not use this file except in compliance with the License. |
6 | * You may obtain a copy of the License at |
7 | * |
8 | * http://www.apache.org/licenses/LICENSE-2.0 |
9 | * |
10 | * Unless required by applicable law or agreed to in writing, software |
kristofer / Memoization: Let’s Be Efficient, Shall We?
0 likes
0 forks
1 files
Last active
Ever ask the same question over and over? Annoying, right? That’s what your computer thinks too.
1 | # example of memoization in python for fibonacci |
2 | |
3 | # Memoization: Let’s Be Efficient, Shall We? |
4 | # Ever worry about recursion and the stack and so on... |
5 | # Ever ask the same question over and over? |
6 | # Annoying, right? That’s what your computer thinks too. |
7 | # Memoization fixes this by storing results so you don’t have to repeat expensive calculations. |
8 | def fib(n, memo={}): |
9 | if n in memo: |
10 | return memo[n] |
kristofer / Ten VMs in Java
0 likes
0 forks
11 files
Last active
Ten Simple Virtual machines, in Java, gen's by ChatGPT-4o
This was the prompt of these 10 Java virtual machines all created by ChatGPT-4o.
[
{'role': 'system', 'content': 'you are an experienced Java programming language developer.'},
{'role': 'user',
'content': 'generate a virtual machine runtime which has an operand stack and a context stack for frames.'}
]