Skip to main content

All Questions

Filter by
Sorted by
Tagged with
59 votes
9 answers
14k views

Project Euler problem 1 in Python - Multiples of 3 and 5

I'd like suggestions for optimizing this brute force solution to problem 1. The algorithm currently checks every integer between 3 and 1000. I'd like to cut as many unnecessary calls to ...
Robert S Ciaccio's user avatar
7 votes
3 answers
3k views

Efficiency of Project Euler problem 35

The number, 197, is called a circular prime because all rotations of the digits: 197, 971, and 719, are themselves prime. There are thirteen such primes below 100: 2, 3, 5, 7, 11, 13, 17, 31, 37, 71, ...
yask's user avatar
  • 245
7 votes
2 answers
4k views

Project Euler - Problem 54: testing poker hands

This was a very fun and thought provoking problem, and I'm quite proud of the way I was able to pull it off. I broke it down into 2 parts, testing and comparing. Testing each group of cards for ...
blitzmann's user avatar
  • 173
7 votes
2 answers
6k views

Character Picture Grid exercise - automatetheboringstuff

Regarding the Character picture exercise located at the end the following page: https://automatetheboringstuff.com/chapter4/ Say you have a list of lists where each value in the inner lists is a ...
drapozo's user avatar
  • 115
3 votes
4 answers
3k views

Sieve of Eratosthenes: making it quicker

I was thinking about doing problem 23 of Project Euler. It includes the difficulty where you have to factorize primes. I had already done that in problems I solved earlier, but it was only necessary ...
user avatar
16 votes
4 answers
3k views

Sieve of Sundaram for Project Euler 7: Python implementation slower than C++ and R

A friend of mine recently started learning R, and she complained to me that R is very slow. She was working on Project Euler Problem 7, which asks for the value of the 10001st prime number. For ...
castle-bravo's user avatar
12 votes
2 answers
4k views

CodeFights: Pipes game

Description Carlos always loved playing video games, especially the well-known computer game "Pipes". Today he finally decided to write his own version of the legendary game from scratch. In ...
Ludisposed's user avatar
  • 11.6k
8 votes
2 answers
6k views

BFS shortest path for Google Foobar "Prepare the Bunnies' Escape"

This is the Google Foobar puzzle "Prepare the Bunnies' Escape": You have maps of parts of the space station, each starting at a prison exit and ending at the door to an escape pod. The map ...
oxtay's user avatar
  • 183
7 votes
2 answers
447 views

Finding min and max values of an iterable on the fly

This is a follow up for my question about optimizing solution for DNA Health HackerRank problem. Short re-cap: You are given 2 arrays (genes and ...
Denis Shvetsov's user avatar
6 votes
2 answers
806 views

4 sum challenge (part 2)

This is a continued discussion from (4 sum challenge) by return count only. Problem Given four lists A, B, C, D of integer values, compute how many tuples (i, j, k, l) there are such that A[...
Lin Ma's user avatar
  • 3,453
6 votes
2 answers
896 views

Typeahead autocomplete functionality challenge

I'm working on a Talent Buddy challenge . In this challenge you have to design the autocomplete functionality of Twitter. You are given a list of queries and possible usernames. You have to return the ...
Olivier_s_j's user avatar
5 votes
3 answers
3k views

Efficiency on Project Euler Problem #8

I am doing Project Euler problem #8: The four adjacent digits in the 1000-digit number that have the greatest product are 9 × 9 × 8 × 9 = 5832. ...
user49487's user avatar
  • 253
4 votes
1 answer
404 views

Counting ways to fit bottles in crates

I'm calculating how many possibilities there are to fit \$n\$ identical bottles inside \$k\$ crates of various capacities. n = Number of bottles k = Number of crates K = List of the number of bottles ...
Deniz Alz's user avatar
2 votes
3 answers
8k views

Finding Pythagorean Triple that sums to 1000

I am doing this Project Euler Question #9: A Pythagorean triplet is a set of three natural numbers, \$a < b < c\$, for which, $$a^2 + b^2 = c^2$$ For example, \$3^2 + 4^2 = 9 + 16 = ...
user49487's user avatar
  • 253
2 votes
2 answers
907 views

Project Euler problem 23 - non-abundant sums

I present my solution for Project Euler problem 23 written in Python (2.7). The problem statement is: A perfect number is a number for which the sum of its proper divisors is exactly equal to ...
mkrieger1's user avatar
  • 1,704
1 vote
1 answer
386 views

Project Euler 34 # Digit Factorials in Python

145 is a curious number, as 1! + 4! + 5! = 1 + 24 + 120 = 145. Find the sum of all numbers which are equal to the sum of the factorial of their digits. Note: as 1! = 1 and 2! = 2 are not sums they are ...
user avatar
32 votes
1 answer
8k views

100 gunmen in a circle kill next person

I am very happy because I solved this problem with very little code: ...
Caridorc's user avatar
  • 27.6k
30 votes
6 answers
72k views

Checking for balanced brackets in Python

I'm solving HackerRank "Stacks: Balanced Brackets" in Python. A bracket is considered to be any one of the following characters: (, ...
vinicius's user avatar
  • 647
27 votes
6 answers
120k views

Project Euler #1: Multiples of 3 and 5

Challenge Description: If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23. Find the sum of all the multiples of ...
MTHL's user avatar
  • 535
26 votes
4 answers
1k views

Generalized Project Euler 1: A sledgehammer to crack a nut

The problem Project Euler 1 is one of the most asked questions on site. However I wanted to solve the more general problem of division. Multiples of a list If we list all the natural numbers ...
N3buchadnezzar's user avatar
25 votes
5 answers
2k views

Count distinct primes, discarding palindromes, in under 2 seconds

Problem Statement Generate as many distinct primes P such that reverse (P) is also prime and is not equal to P. Output: Print per line one integer( ≤ 1015 ). Don't print more than 106 ...
Akash Rana's user avatar
18 votes
5 answers
3k views

Flipping coins performance

This is the Flipping coins problem on CodeChef: There are \$N\$ coins kept on the table, numbered from \$0\$ to \$N - 1\$. Initially, each coin is kept tails up. You have to perform two types of ...
Ukiyo's user avatar
  • 283
15 votes
5 answers
9k views

Google Foobar Challenge: Lucky Triples

Note: Since my time has passed for this challenge, I do not remember exactly the stipulations but will attempt to recapitulate them to the best of my knowledge. Essentially, the challenge was this: ...
Edwin Rice's user avatar
15 votes
2 answers
1k views

Project Euler #22 - Names Scores

I programmed Problem #22 from Project Euler in Python. It works but I want to know if it really is pythonic enough. Using names.txt (right click and 'Save Link/Target As...'), a 46K text file ...
magu_'s user avatar
  • 938
14 votes
4 answers
13k views

Google Foobar Challenge: Save Beta Rabbit in Python

I'm going through the Google Foobar challenges and am currently on the first challenge of level three. I've come up with a working solution, however, it's not an acceptable solution because it runs ...
Andrew's user avatar
  • 243
14 votes
1 answer
322 views

Recover flights from found tickets

I just found a whole bunch of plane tickets that only have a start and destination on them. I also know the route started in a certain city. I would like to recover the (alphabetically) first possible ...
redfast00's user avatar
  • 459
13 votes
5 answers
9k views

"Algorithmic Crush" problem hitting timeout errors

This is the HackerRank problem Algorithmic Crush: You are given a list of size N, initialized with zeroes. You have to perform ...
enthusiastic's user avatar
13 votes
7 answers
3k views

Find largest prime factor

Here is my attempt at solving Project Euler problem 3. It works for test numbers up to 9 digits long, but overflows when I input the real 12-digit behemoth. I've looked at other methods of solution, ...
yaegermenjensen's user avatar
13 votes
5 answers
2k views

Array manipulation: add a value to each of the array elements between two given indices

This is a Hackerrank problem: https://www.hackerrank.com/challenges/crush/problem You are given a list of size \$N\$, initialized with zeroes. You have to perform \$M\$ operations on the list and ...
jeremy radcliff's user avatar
13 votes
3 answers
4k views

Codewars: Reduce strings to one character

I am trying to solve a puzzle in a performant way. This is the puzzle. Simply explained: you have a String and you must reduce it to one character with the given rules. This is the solution to the ...
Superluminal's user avatar
12 votes
3 answers
16k views

Project Euler Problem 12 in Python

I'm trying to do Project Euler Problem 12, which reads as: The sequence of triangle numbers is generated by adding the natural numbers. So the 7th triangle number would be 1 + 2 + 3 + 4 + 5 + 6 + ...
Ali's user avatar
  • 223
12 votes
1 answer
1k views

Print the string equivalents of a phone number

Task Old mobile phones had the ability to type characters by pressing a number. The letter a could be typed by pressing 2 once. ...
MrJoe's user avatar
  • 2,053
11 votes
2 answers
948 views

Milking order compatible with maximum number of observations

I'm using some old USA Computing Olympiad (USACO) problems to help teach me. This is the "Milking order" problem: Farmer John has \$N\$ cows (\$1≤N≤10^5\$), numbered \$1…N\$. He has made \$M\$ ...
silverware's user avatar
10 votes
4 answers
3k views

First non-repeating Character, with a single loop in Python

I recently tried to solve the first non-repeating character problem. Please tell me if my solution is valid. I'm aiming for O(n) with a single loop. My thinking is, it would be easy to tell you what ...
zing's user avatar
  • 203
10 votes
2 answers
2k views

Pangrams CodeEval challenge

I took a challenge on CodeEval. Although the code seems to work for the examples taken from the site, I feel it is not really pretty and must be more complicated than it should be. Description: ...
Tommy's user avatar
  • 225
10 votes
5 answers
853 views

Minimum perfect squares needed to sum up to a target

I'm trying to solve this problem Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9, 16, ...) which sum to n. I've come up with a solution that ...
nz_21's user avatar
  • 1,031
10 votes
4 answers
2k views

Wordle puzzle game

I am a beginner programmer with not much experience, the little I do have is with Python specifically. With all the fuss going around about the popular word game "Wordle" I wanted to ...
Erikson Thomas's user avatar
10 votes
5 answers
4k views

Leetcode two sum

I'm currently learning c++ coming from a python background, so I'll include a solution in python and in c++ for the following problem statement: Given an array of integers nums and an integer target, ...
watch-this's user avatar
8 votes
1 answer
3k views

Project Euler 407: Is there any more optimal way to solve this idempotent equation (modulo n ring)?

Project Euler problem 407: If we calculate a2 mod 6 for 0 ≤ a ≤ 5 we get: 0, 1, 4, 3, 4, 1. The largest value of a such that a2 mod 6 = a is 4. Let's call M(n) the largest value of a < n ...
Ilia Sucholutsky's user avatar
8 votes
2 answers
297 views

Summation of primes for large inputs

I'm doing a problem from Project Euler to find the sum of all primes under two million. My code manages to get a pretty good result for small inputs, but when trying inputs like million or two it ...
galah92's user avatar
  • 223
8 votes
1 answer
2k views

Befunge-93 interpreter in Python

I was doing some CodeFights and had alot of fun with this Befunge challange Problem statement While exploring the ruins of a golden lost city, you discovered an ancient manuscript containing ...
Ludisposed's user avatar
  • 11.6k
8 votes
2 answers
1k views

Leetcode longest substring without repeating characters

Link here I'm currently learning c++ coming from a python background, so I'll include a solution in python and in c++ for the following problem statement and based on very helpful answers obtained on ...
watch-this's user avatar
8 votes
4 answers
2k views

Coin Flip Streaks - correct streak condition

This is a practice task from Automate the Boring Stuff with Python. I imagine many others have asked for their version of the solution to be checked, so I apologise beforehand for boring you yet ...
aLearner's user avatar
8 votes
1 answer
17k views

Shortest Path For Google Foobar (Prepare The Bunnies Escape)

I have been working on Google Foobar since a few days ago. I am currently in the third level but is stuck in the second challenge of "Prepare the Bunnies' Escape." I have checked this post but it did ...
Sean Francis N. Ballais's user avatar
8 votes
2 answers
749 views

Sum of prime factors of binomial coefficients over 9000!\${}\$

The problem I have lately been working Project Euler: 231: The prime factorisation of binomial coefficients The binomial coefficient \$ ^{10}C_3 = 120 \$. \$ 120 = 2^3 × 3 × 5 = 2 × 2 ×...
N3buchadnezzar's user avatar
7 votes
1 answer
2k views

Python program to find a word ladder transforming "four" to "five"

I saw this Puzzling problem and thought I would try to write a Python program to solve it. The task is to transform "four" to "five", forming a new four-letter word at each step, replacing one letter ...
Alex F's user avatar
  • 443
7 votes
6 answers
2k views

Generalized Project Euler #4: Largest palindrome from product of two n-digit numbers in Python

This solves Project Euler 4: Largest palindrome product using Python (not limited to 3 digit numbers). I need suggestions for improvements to either the Python code or the math/algorithm since time of ...
Amey Dahale's user avatar
7 votes
2 answers
7k views

Decode the Morse Code

From Code Wars: This kata is part of a series on the Morse code. After you solve this kata, you may move to the next one. In this kata you have to write a simple Morse code decoder. While the ...
Tobi Alafin's user avatar
  • 1,752
7 votes
2 answers
6k views

Sherlock and The Beast

I have recently written the program for the Sherlock and The Beast' HackerRank challenge. That's working fine, but the problem is that it takes too much time if a big number is given as a input. I ...
Mohammad Areeb Siddiqui's user avatar
7 votes
2 answers
2k views

Helper function to solve Project Euler question 26

Project Euler problem 26 asks us to: Find the value of d < 1000 for which 1/d contains the longest recurring cycle in its decimal fraction part. I wrote this function in Python to find the ...
ajay's user avatar
  • 295