kristofer hat die Gist bearbeitet . Zu Änderung gehen
1 file changed, 111 insertions
binning.md(Datei erstellt)
| @@ -0,0 +1,111 @@ | |||
| 1 | + | ## Binning of frequencies | |
| 2 | + | ||
| 3 | + | ||
| 4 | + | Examples of python and java of using dict/hashmap for binning of 1) dice rolls, | |
| 5 | + | 2) letter frequency and 3) checking for existing keys. | |
| 6 | + | ||
| 7 | + | ### Key patterns: | |
| 8 | + | ||
| 9 | + | - Python: dict.get(key, default) and defaultdict(int) are idiomatic | |
| 10 | + | - Java: map.getOrDefault(key, default) and map.merge() are modern approaches | |
| 11 | + | - Both languages support checking key existence before accessing | |
| 12 | + | ||
| 13 | + | ||
| 14 | + | ### in Python | |
| 15 | + | ||
| 16 | + | ```python | |
| 17 | + | # 1) Dice rolls - counting occurrences | |
| 18 | + | from collections import defaultdict | |
| 19 | + | import random | |
| 20 | + | ||
| 21 | + | # Using defaultdict (cleaner) | |
| 22 | + | dice_counts = defaultdict(int) | |
| 23 | + | for _ in range(100): | |
| 24 | + | roll = random.randint(1, 6) | |
| 25 | + | dice_counts[roll] += 1 | |
| 26 | + | # Result: {1: 15, 2: 18, 3: 17, 4: 16, 5: 19, 6: 15} | |
| 27 | + | ||
| 28 | + | # Using regular dict | |
| 29 | + | dice_counts = {} | |
| 30 | + | for _ in range(100): | |
| 31 | + | roll = random.randint(1, 6) | |
| 32 | + | dice_counts[roll] = dice_counts.get(roll, 0) + 1 | |
| 33 | + | ||
| 34 | + | # 2) Letter frequency | |
| 35 | + | text = "hello world" | |
| 36 | + | letter_freq = {} | |
| 37 | + | for char in text.lower(): | |
| 38 | + | if char.isalpha(): | |
| 39 | + | letter_freq[char] = letter_freq.get(char, 0) + 1 | |
| 40 | + | # Result: {'h': 1, 'e': 1, 'l': 3, 'o': 2, 'w': 1, 'r': 1, 'd': 1} | |
| 41 | + | ||
| 42 | + | # 3) Checking for existing keys | |
| 43 | + | inventory = {'apples': 5, 'bananas': 3} | |
| 44 | + | ||
| 45 | + | # Method 1: using 'in' | |
| 46 | + | if 'apples' in inventory: | |
| 47 | + | print(f"Have {inventory['apples']} apples") | |
| 48 | + | ||
| 49 | + | # Method 2: using get() with default | |
| 50 | + | oranges = inventory.get('oranges', 0) # Returns 0 if not found | |
| 51 | + | ||
| 52 | + | # Method 3: try/except | |
| 53 | + | try: | |
| 54 | + | count = inventory['oranges'] | |
| 55 | + | except KeyError: | |
| 56 | + | count = 0 | |
| 57 | + | ``` | |
| 58 | + | ||
| 59 | + | ### in Java | |
| 60 | + | ||
| 61 | + | ```java | |
| 62 | + | import java.util.*; | |
| 63 | + | ||
| 64 | + | // 1) Dice rolls - counting occurrences | |
| 65 | + | Map<Integer, Integer> diceCount = new HashMap<>(); | |
| 66 | + | Random rand = new Random(); | |
| 67 | + | ||
| 68 | + | for (int i = 0; i < 100; i++) { | |
| 69 | + | int roll = rand.nextInt(6) + 1; | |
| 70 | + | diceCount.put(roll, diceCount.getOrDefault(roll, 0) + 1); | |
| 71 | + | } | |
| 72 | + | // Result: {1=15, 2=18, 3=17, 4=16, 5=19, 6=15} | |
| 73 | + | ||
| 74 | + | // Alternative using merge() | |
| 75 | + | for (int i = 0; i < 100; i++) { | |
| 76 | + | int roll = rand.nextInt(6) + 1; | |
| 77 | + | diceCount.merge(roll, 1, Integer::sum); | |
| 78 | + | } | |
| 79 | + | ||
| 80 | + | // 2) Letter frequency | |
| 81 | + | String text = "hello world"; | |
| 82 | + | Map<Character, Integer> letterFreq = new HashMap<>(); | |
| 83 | + | ||
| 84 | + | for (char c : text.toLowerCase().toCharArray()) { | |
| 85 | + | if (Character.isLetter(c)) { | |
| 86 | + | letterFreq.put(c, letterFreq.getOrDefault(c, 0) + 1); | |
| 87 | + | } | |
| 88 | + | } | |
| 89 | + | // Result: {h=1, e=1, l=3, o=2, w=1, r=1, d=1} | |
| 90 | + | ||
| 91 | + | // 3) Checking for existing keys | |
| 92 | + | Map<String, Integer> inventory = new HashMap<>(); | |
| 93 | + | inventory.put("apples", 5); | |
| 94 | + | inventory.put("bananas", 3); | |
| 95 | + | ||
| 96 | + | // Method 1: using containsKey() | |
| 97 | + | if (inventory.containsKey("apples")) { | |
| 98 | + | System.out.println("Have " + inventory.get("apples") + " apples"); | |
| 99 | + | } | |
| 100 | + | ||
| 101 | + | // Method 2: using getOrDefault() | |
| 102 | + | int oranges = inventory.getOrDefault("oranges", 0); // Returns 0 if not found | |
| 103 | + | ||
| 104 | + | // Method 3: checking null | |
| 105 | + | Integer count = inventory.get("oranges"); | |
| 106 | + | if (count == null) { | |
| 107 | + | count = 0; | |
| 108 | + | } | |
| 109 | + | ``` | |
| 110 | + | ||
| 111 | + | ||
Neuer
Älter