ggorlen
  • Member for 3 years, 7 months
  • Last seen this week
Efficient renaming of dict keys from another dict's values - Python
Accepted answer
19 votes

I'd use a dict comprehension: pretty_dict = {replacement_keys[k]: v for k, v in ugly_dict.items()} This throws an error if replacement_keys (keyMapping) is missing any k. You might want to handle ...

View answer
Leetcode Two Sum code in Python
18 votes

Code Style Your code contains a few lines that accomplish nothing and obfuscate your intent: else: continue If the conditional is false, you'll automatically continue on to the next ...

View answer
Simple Tic Tac Toe game inside terminal
Accepted answer
15 votes

On the whole this is nicely done for an application made during your first week of C programming. The functions are generally reasonably-sized, the code is comprehensible and the design decisions are ...

View answer
Is there a simpler way to sum the lengths of arrays within an array of arrays in JavaScript?
Accepted answer
13 votes

There's no need for the inner loop to compute the length element by element. It's better to use the .length property on each row, reducing the inner loop time complexity to O(1), and covert your C-...

View answer
Easy to read palindrome checker
Accepted answer
12 votes

Time complexity Your time complexity is linear but you can save a few traversals over the string and lower the constant factor as you improve readability. Checking whether a string is a palindrome ...

View answer
Convert camelCase and PascalCase to Title Case
Accepted answer
11 votes

I like your solution quite a bit. It's clear, easy to read and I don't see any bugs. There are many ways to condense the replace calls as you mention, but I think you're at a point where such ...

View answer
A function which translates a sentence to title-case
Accepted answer
11 votes

I find this code quite reasonable on the whole, but you might consider the following suggestions relating to consistency, succinctness and semantics. Move and rename the inner helper function ...

View answer
Gomoku (Connect 5) game implementation in pygame
Accepted answer
10 votes

On the whole, this game works nicely and the design decisions make sense for the size and purpose of the program. Variable names are clear, there's some separation between GUI and game logic and you'...

View answer
Getting a key's value in a nested dictionary
Accepted answer
10 votes

Great job so far! I have a few suggestions for your code: Avoid writing functions with side effects such as printed output. Side effects make a function much less reusable. Instead, you may return a ...

View answer
Cursed error generator in Javascript
Accepted answer
6 votes

Efficiency Since there is no real algorithm here to speak of, all optimizations are micro-optimizations and boil down to avoiding unnecessary object allocations. Every call to genCursedError allocates ...

View answer
Javascript - Find a deepest node in a binary tree
Accepted answer
6 votes

Here are my thoughts: Don't mutate function parameters unless there is good reason to do so. node.depth = level; The above statement basically breaks the contract of the function. The function ...

View answer
Fisher Yates Shuffle, but using Javascript Array Functions
Accepted answer
5 votes

This code is overly terse, clever and (in the bottom two examples) broken. The versions with loops are far more readable, correct/verifiable, efficient and intuitive to understand. Don't cache array....

View answer
Constructor function for persons with hobbies
Accepted answer
5 votes

Performance Strings are immutable, so using accumulator += stuffToAppend in a loop can traditionally impact performance. The problem is that we're creating a new string every iteration, leading to ...

View answer
Recording all instances of a class - Python
5 votes

I recommend keeping the collection of instances of a class public until there is compelling reason to hide such information from the client, and I see no such motivation here. Furthermore, I'm not ...

View answer
Reverse Integer
5 votes

I'd just use string manipulation on this, with a simple conditional for the edge cases. var reverse = n => { const s = parseInt([..."" + n].reverse().join("")); return s >= 2 ** 31 ? 0 : ...

View answer
Cliché Rock, Paper, Scissor, as first Python program
4 votes

First of all, great work! The program appears solid, the interface is decent and you handle bad input. I have a few suggestions for improvement: UI/UX The "1" "2" "3" &...

View answer
JS function to initialize properties
4 votes

TL;DR This code snippet is rather taken out of context and is likely symptomatic of bigger issues in the codebase as a whole. You may have bigger problems than trying to improve these few lines of ...

View answer
A Python Snake Game Using Pygame
Accepted answer
4 votes

On the whole, the game works and has a solid UI that you've correctly kept basic and mostly out of play as you focus on the game engine logic. It looks like you've gone for a partially-OOP approach by ...

View answer
CTCI Chapter 1 : Palindrome Permutation
4 votes

This looks correct! Here are my thoughts on the code: Per PEP-8, use snake_case for variable names and PascalCase for class names. Use Python builtins. Python makes frequency counting effortless ...

View answer
HackerRank New Year Chaos: arriving at a given list using neighbor swaps
Accepted answer
4 votes

Good attempt, but unfortunately the nested loop brings the time complexity to O(n2). Keep in mind that .find performs a linear search on the vector, inspecting up to the entire array to find an ...

View answer
Longest Substring Without Repeating Characters in JS
Accepted answer
4 votes

Style Nice usage of arrow functions and spread operators. You can extend this to the function name as well: const lengthOfLongestSubstring = s => { ... }; The code switches between two and four ...

View answer
Singly Linked List Implementation in ES6
4 votes

TL;DR Style could better adhere to established standards. insertAt has bugs to resolve. This class is missing basic add/remove functions which prevent it from being useful. Writing a linked list ...

View answer
Arithmetic quiz on the command line
3 votes

This code is WET (wrote everything twice) and hard to extend/reuse. What if you want to ask, say, 10 questions? How about 100? Following the original design would likely create a pyramid of doom. We ...

View answer
A javascript script to load a list into a HTML page
Accepted answer
3 votes

A couple of good points to start out: You use for..of loops... great! You use fetch... great! Suggestions for improvement: Naming JS uses snakeCase, not snake_case. It's OK to use snake_case for ...

View answer
Recursive function to find the number of ways a number can be generated out of a set of numbers
Accepted answer
3 votes

I'm assuming in this answer that you have infinite quantities of the designated numbers at your disposal, since that appears to be the way your code works. With that condition, this problem is the ...

View answer
basic paging function, is there logical problems with the JavaScript part?
2 votes

UX Great job on the end product! There are no distracting CSS animation frills and everything appears to work smoothly. Consider disabling the forward/backward arrows on the first and last pages ...

View answer
Javascript Pub/Sub
Accepted answer
2 votes

Style nitpicks Be consistent with Allman vs Egyptian braces: constructor() { this.name = "name"; sd.register(this, "List:1234", "print", this.printName); } should ...

View answer
LeetCode: Deepest Leaves Sum C#
Accepted answer
2 votes

This solution seems generally fine although it's not much code to analyze and it's already as succinct as it could reasonably be. A few suggestions: Avoid naming a class SomethingClass. It's enough ...

View answer
Calculator App with Kotlin and Android Studio
1 votes

Crash Dividing by 0 crashes the app ungracefully. Detect this and show an error message. Warnings Check your "Problems" tab in Android studio. It tells you to use @string resources rather ...

View answer
Bidirectional iterate over list in Python
1 votes

Your code is good from a readability standpoint. I appreciate the type hints. My only minor suggestions are using if __name__ == "__main__": for your driver code and lst instead of arr (arr ...

View answer