All Questions
Tagged with programming-challenge python
123 questions
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 ...
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, ...
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 ...
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
...
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 ...
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 ...
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 ...
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 ...
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 ...
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[...
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 ...
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.
...
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 ...
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 = ...
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 ...
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 ...
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:
...
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: (,
...
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 ...
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 ...
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 ...
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 ...
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 ...
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:
...
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 ...
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 ...
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 ...
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 ...
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, ...
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 ...
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 + ...
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. ...
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\$ ...
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 ...
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, ...
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:
...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ×...
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 ...
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 ...
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 ...
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 ...
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 ...
7
votes
2
answers
3k
views
Prime factorization for integers with up to 9-digit long prime factors
I've become interested prime factorization since solving Project Euler problem 3 (finding the largest prime factor of 600851475143). Learning here that initializing lists with many elements to later ...