New answers tagged

0 votes

Python Genetic Algorithm Implementation

...
user278346's user avatar
0 votes

'Snakes and Ladders' game

def move(player_name, start_pos): snake_squares = {16: 4, 33: 20, 48: 24, 62: 56, 78: 69, 94: 16} ladder_squares = {3: 12, 7: 23, 20: 56, 47: 53, 60: 72, 80: 94} ...
sching cogn's user avatar
-2 votes

NLP pre-processing function optimization as it is extremely low on 92Mb data set

Consider following tips: Use List/Dict: Avoid running numerous basic string operations on large class objects like DataFrames and ...
Parfait's user avatar
  • 911
1 vote

Optimizing __getitem__ for a bioinformatics script in Python

new to the area and I've been learning while developing Great, welcome! Software is almost as interesting as biology. And it's much less messy. measurements much longer for larger files “When you ...
J_H's user avatar
  • 16.6k
-1 votes

Number to text in Python

dict comprehension with library Using existing libraries, essentially "standing on the shoulders of giants", allows us to create stronger code. num2words is a library to convert numbers to ...
Attila Vajda's user avatar
5 votes

Number to text in Python

one dict or three Using num_to_words_first_numbers, ten_by_ten, and spelled_numbers was an interesting design choice. It is certainly fine to leave it as is. But another approach would pre-populate <...
J_H's user avatar
  • 16.6k
2 votes

Number to text in Python

You can do it with a one-liner once defined the initial values: ...
mattiatantardini's user avatar
3 votes

Dictionary additional function implementations

As @FMc already commented, your method __add__() assumes that both dicts have the same keys at the same positions, which is a very bold assumption. Furthermore, the ...
Richard Neumann's user avatar
2 votes
Accepted

Is it a correct way to read a file contents using paramiko (SSH, SFTP client)?

There's a pair of high-level isssues with this design. declaring object attributes object (session) lifetimes You offered example usage; thank you. I'm going to pretend it was this slightly more ...
J_H's user avatar
  • 16.6k
3 votes
Accepted

Data structure to hold elements for script execution

say what you mean and mean what you say. Alice, when she's through the looking glass, is admonished by the March Hare: you should say what you mean and things go downhill from there. ...
J_H's user avatar
  • 16.6k
1 vote

Receive byte string (high speed position output) from FANUC robot, parse and append to dated .xml file

First and foremost, the socket data you retrieve is not actually XML but a markup that resembles XML and your resulting file output is more a .txt than ...
Parfait's user avatar
  • 911
0 votes

Dictionary additional function implementations

Interesting class. What motivated it? You might come up with some more descriptive name than BetterDict, perhaps a name drawn from the business domain that ...
J_H's user avatar
  • 16.6k
2 votes

Pentomino solver in Python

Kudos on tackling such an interesting problem! design of Public API ...
J_H's user avatar
  • 16.6k
0 votes

Dynamic python config reader

Is Something Missing? It seems to me something is missing from your code. You say in part: Now I can import 'preconfigured' in any other file, and pass any preconfigured CustomClass instance around .....
Booboo's user avatar
  • 749
1 vote

HackerRank Algorithm Problem: Climbing the Leaderboard (Python)

My approach, described below, should definitely be an improvement. I have named my version climbing_leaderboard so as to not conflict with your name (I have ...
Booboo's user avatar
  • 749
1 vote

Python GUI Based IP location digger

The try / except ImportError / pip install all needs to go away. There are good ways to capture program requirements, but this is not one of them. Write the ...
Reinderien's user avatar
0 votes

Dynamic python config reader

I like it! It looks good. finding sections for section in config.sections(): if section.startswith('DefaultInstance'): If you need to do such prefix ...
J_H's user avatar
  • 16.6k
2 votes
Accepted

Settings and global variables in one place

Another thing I don't like is the long name when using dictionary notation config['parameter1']['parameter2']. Consider using the glom library, which was designed ...
J_H's user avatar
  • 16.6k
1 vote

Chess Dictionary Validator (Python) (Automate The Boring Stuff)

This code will not work if in the board multiple pieces have the same position. For example, below board contains 2 white pawns in the same position "3a". Even though the board is an invalid ...
tom varghese's user avatar
2 votes
Accepted

Theme Toggle switch refactoring

I am not sure your code is ready for review. I have some comments that you might wish to consider if you will be further updating your code. But these comments are too long and too many to be posted ...
Booboo's user avatar
  • 749
1 vote

Trailing Digits

finally I found a link with a lot of examples. I found trailingdigits.py with a much more elegant approach github.com/RussellDash332/kattis/blob/main/src/… and another webpage that shows more detail ...
qleoz12's user avatar
  • 141
2 votes

Split netmask or ip address

Faulty documentation Your docstring states: Splits an IPv4 interface address including an optional network mask into it's octets. [...] This is false, snce the network mask is not optional according ...
Richard Neumann's user avatar
2 votes

Trailing Digits

Picture yourself coming back to "V1 code" a decade from now: You can read it, you can run it. Do you know what does, and to what end? Name things for what they are good for. ...
greybeard's user avatar
  • 6,104
2 votes

Finding a distinct triplet where the sum of the values is divisible by a given number

Its possible to solve the problem in O(N + D*log(D)) time, using the Fast Fourier Transform (FFT). In a nutshell- reduce each value mod ...
Dillon Davis's user avatar
0 votes

GUI Tic Tac Toe game with UNBEATABLE AI players

This is a tiny review that focuses only on logic2.py and train_ai.py. There are a lot of int...
Setris's user avatar
  • 1,503
3 votes
Accepted

Cleaning Yaml Data in Python

nouns and verbs We use nouns for class names and verbs for function (or method) names. ...
J_H's user avatar
  • 16.6k
1 vote

Finding a distinct triplet where the sum of the values is divisible by a given number

I'd prefer divisible_triplets(integers, divisor). And it deserves a docstring. You state I want to count the number of … - I don't want to be caught telling people ...
greybeard's user avatar
  • 6,104
3 votes
Accepted

Clock markings refactoring for enhancement opportunities

Wow, that's a lot of imported symbols at the module level. I guess it works for you. You might consider defining a class, so ...
J_H's user avatar
  • 16.6k
2 votes

Split netmask or ip address

There's two things to consider here: is it correct? and is it pythonic? Currently it's 2023, we've moved on a bit from 1981. "IP" means something, and it's not what your code is working ...
J_H's user avatar
  • 16.6k
2 votes

Finding a distinct triplet where the sum of the values is divisible by a given number

One of the weaknesses of the code is that we're actually finding every triple. We're not required to do so - the only thing that matters is the count of results. That means that we can group the ...
Toby Speight's user avatar
3 votes
Accepted

Refactoring random time values list generator function

What you've described and what you've written are two different things. You've described a function that outputs random times in a list, but it actually outputs a list of hour-minute tuple values ...
Reinderien's user avatar
2 votes

Finding a distinct triplet where the sum of the values is divisible by a given number

We can preprocess the array by setting each a_i to a_i modulo d. We're asked for a sum of ...
J_H's user avatar
  • 16.6k
1 vote

Finding a distinct triplet where the sum of the values is divisible by a given number

One improvement might be derived from changing every array entry to its modulo 5 value: ...
Joop Eggen's user avatar
  • 3,376
1 vote
Accepted

Simplifying and enhancing readability in time angle calculation

This isn't bad, it's perfectly serviceable code. The docstring should be an English sentence. Each sentence ends with a . period. Representing ...
J_H's user avatar
  • 16.6k
2 votes
Accepted

Password database: saving and loading encrypted files

Does this code follow best practices when it comes to security? No. This is SOTA from a few decades ago. Passwords have low entropy, typically much less than 256 bits. Modern implementations will ...
J_H's user avatar
  • 16.6k
2 votes

Python (API requests)

Here goes the new code following @J_H advices: Project structure ...
Aldimar J's user avatar
2 votes

Project Euler 60: Prime Pair Sets

Eliminate 2 and 5 from your sieve! Any 2+ digit prime cannot end in a 2 or a 5, as these will be multiples of ...
AJNeufeld's user avatar
  • 33.3k
4 votes

HackerRank Project Euler 12 (Python) | Highly Divisible Triangular Numbers

Inaccuracy Many Project Euler problems deal with BIG numbers. These often are large enough that int(number**0.5) will give you the incorrect result! Mathematically,...
AJNeufeld's user avatar
  • 33.3k
2 votes
Accepted

Find the list of combination (with repetition) given rank and number of items. (Lexicographic ordering)

First Take Your code gets the job done and is correct as far as I can tell. It would be more convenient if it was converted to a function. I have taken the liberty to do so as follow: ...
Joseph Wood's user avatar
1 vote
Accepted

delete rows based on references to other tables in a mysql database?

nit: For consistency with scan.id I would choose the column name scan_id instead of id_scan, ...
J_H's user avatar
  • 16.6k
2 votes

Project Euler 10(Python) | Summation of Primes

It was clarified that this problem is the Hacker Rank version: https://www.hackerrank.com/contests/projecteuler/challenges/euler010/problem This problem has the format of multiple test cases being ...
harold's user avatar
  • 9,049
1 vote

Python: Detect if a number is divisible by any number in a list

i is not divisible by a factor of step_size iff gcd(i, step_size) == 1. Otherwise, that gcd ...
harold's user avatar
  • 9,049
2 votes

Python: Detect if a number is divisible by any number in a list

natural log I'm unhappy with this expression: prime_count = ... len(str(number)) // 3 Since the length computes log10(), and <...
J_H's user avatar
  • 16.6k
3 votes

Python strong password validator ( with unit test )

Please read this as an addition to two existing answers, I agree with almost all points made there. Whitespace You may like excessive vertical whitespace, it's your choice. But in this case it's a ...
SUTerliakov's user avatar
3 votes
Accepted

Python (API requests)

In a pathname like logs/err-logs.log, the -logs suffix seems redudnant. one level down Based on ...
J_H's user avatar
  • 16.6k
4 votes

Python strong password validator ( with unit test )

Style Your code is readable, well documented, and mostly follows conventions, nice. My only nitpicks here is that you use snake_case for constants, where these ...
gazoh's user avatar
  • 3,004
2 votes
Accepted

Distance on a unit grid

min_x = min(abs(x1 - x2), dist_x = (x1 - x2) ** 2 We can see the border cross algorithm and the standard algorithm are ...
Peilonrayz's user avatar
  • 42.7k
11 votes

Python strong password validator ( with unit test )

standard libraries are, umm, standard ...
J_H's user avatar
  • 16.6k
3 votes
Accepted

Convert 64-bit Gray code to integer

While it's not your code, I'll point out that the routine from OEIS can be significantly shortened by using an assignment expression, available since Python 3.8: ...
benrg's user avatar
  • 466
1 vote
Accepted

Extending die roll simulations for complex data science tasks

Pandas should not be used for this purpose. Just use bare Numpy. Do not generate random numbers one at a time. Use the vectorised Numpy random number generator. Rather than ...
Reinderien's user avatar

Top 50 recent answers are included