All Questions
Tagged with python programming-challenge
942
questions
60
votes
10answers
4k views
Using separate functions for Project Euler 1
I started programming with Java and C++, so I'm used to having a 'main' function that calls other functions that do the actual work. At university I was always told that doing actual computation in ...
56
votes
9answers
13k 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 ...
32
votes
8answers
5k views
Project Euler Problem 45
As a self-teaching Python beginner for almost 4 months, I have mostly been doing online challenges including Project Euler problems.
Problem 45 asks:
Triangle, pentagonal, and hexagonal numbers ...
31
votes
1answer
7k views
100 gunmen in a circle kill next person
I am very happy because I solved this problem with very little code:
...
30
votes
6answers
68k 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
6answers
83k 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
2answers
5k views
Printing 1,000,000 numbers in 1 sec. in Python
Here's a fairly simple task from CSES Problem Set - Permutations 1070 that reads:
A permutation of integers 1,2, …, n is called beautiful if there are no adjacent elements whose difference is 1.
...
25
votes
2answers
2k views
Speed up solution to Project Euler problem 75
I wrote this code for Project Euler problem 75, which asks how many integers ≤ 1500000 exist, where the integer is a perimeter length that can be divided into three integer-length sides of a right ...
25
votes
5answers
1k 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 ...
25
votes
4answers
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 ...
23
votes
5answers
16k views
Finding the 10001st prime
I'm solving Project Euler problem 7, which says:
By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13. What is the 10001st prime number?
Here's the ...
18
votes
5answers
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 ...
17
votes
2answers
2k views
Yandex programming contest: Alarms
I've tried to solve this challenge about 4 hours during the contest, but all my attempts exceeded the time limit. I tried to solve it with min-heap, but can't pass all the tests. How it can be solved ...
16
votes
6answers
4k views
Sherlock and The Beast
This is my solution to this question from Hackerrank.
Given N (<= 100000), find the largest N-digit number such that:
The number has only 3 and 5 as its digits.
Number of times 3 ...
16
votes
2answers
2k views
Translate English to Pig Latin
This is a program for converting English to the pseudo-language Pig Latin. Not the strictest form of Pig Latin but it most likely will accurately output an argument in Pig Latin. I got the idea from a ...
15
votes
5answers
4k views
Project Euler # 25 The 1000 digit Fibonacci index
The Fibonacci sequence is defined by the recurrence relation:
Fn = Fn−1 + Fn−2, where F1
= 1 and F2 = 1.
Hence the first 12 terms will be:
F1 = 1
F2 = 1
F3 = 2
F4 = 3
F5 = 5
...
15
votes
6answers
3k views
Project Euler, problem # 9, Pythagorean triplet
Project Euler #9, Pythagorean triplet is
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 = 25 = 5^...
15
votes
4answers
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 ...
15
votes
4answers
6k 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:
...
15
votes
2answers
3k views
Roman to Integer
I'm trying to get better as a programmer and would love any feedback on my code. Especially the section in the while loop. How could I make this more readable? What would your opinion as an ...
15
votes
2answers
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 ...
14
votes
5answers
3k views
Advent of Code 2019: Day 4
Advent of Code 2019: Day 4
I'm doing Advent of Code this year. Below is my attempt at day 4:
Problem
Part One
--- Day 4: Secure Container ---
You arrive at the Venus fuel depot only to discover it's ...
14
votes
4answers
4k views
100-doors puzzle
Here is a very simple solution to the 100 doors challenge
You are in a hotel with 100 doors. Initially every door is closed. On the first round, you change the state of every door (if it is open, ...
14
votes
4answers
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 ...
14
votes
1answer
283 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
1answer
2k views
3-Sum Problem in Python
I attempted the 3-Sum problem on Leetcode, where the problem asks to find all possible triplets of numbers in a given list such that their sum is 0. My code worked, but it exceeded the time limit for ...
13
votes
7answers
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
7answers
11k views
Simplifying a Scrabble Word Finder - Python
I'm very new to coding and am trying to work through projects for practical review of what I know, but I would like to be able to simplify/improve what I've made.
This is a project I saw online that ...
13
votes
3answers
37k views
GCD using Euclid algorithm
Below is the problem taken from here.
Question 10: GCD*
The greatest common divisor of two positive integers a and b is the largest integer which evenly divides both numbers (with no remainder)...
13
votes
2answers
4k views
Average spam confidence
Exercise 7.2 from Python for Informatics:
Write a program to prompt for a file name, and then read through
the file and look for lines of the form:
...
13
votes
5answers
8k 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 ...
13
votes
5answers
965 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
2answers
3k views
Word Morph (changing one letter at a time)
Below is my implementation of Word Morph solver in Python.
The challenge consists of transforming one word into another word by changing one letter at a time. Each word in the chain should be an ...
13
votes
3answers
7k views
Maze solver and generator in Python
After watching Computerphile's video I decided to create my own maze solver and generator in Python. I've never written anything in Python so I'd like to get some feedback about my code, specifically ...
13
votes
2answers
858 views
Beginner Coin Flip project
I think I finally finished a small coin flip project I found online. However, I'm pretty confident this is far from the optimal solution and would really appreciate any feedback on how this could be ...
13
votes
1answer
7k views
Optimizing “Poker hands” challenge solution
I'm trying to solve the project Euler problem 54.
In the card game poker, a hand consists of five cards and are ranked, from lowest to highest, in the following way:
High Card: Highest ...
13
votes
2answers
635 views
A classy DNA nucleobase counter
This question is part of a series solving the Rosalind challenges. For the previous question in this series, see Counting nucleobases in a nucleotide c. The repository with all my up-to-date solutions ...
12
votes
2answers
2k views
“Longest Collatz sequence” in C slower than in Python 3
This tight-looped memoization-less C solution to Project Euler's problem 14 seems efficient, but ran much slower than a similarly trivial Python 3 program. For the desired ...
12
votes
3answers
3k 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 ...
12
votes
2answers
4k views
Make a beautiful binary string
This is the "Beautiful Binary String" problem at HackerRank:
Alice has a binary string. She thinks a binary string is beautiful if and only if it doesn't contain the substring 010.
In one ...
12
votes
4answers
26k views
Python Solution for Project Euler #2 (Fibonacci Sums)
I'm a fairly new programmer (just started yesterday!). I decided to tackle Project Euler #2 today, as I did #1 yesterday without many problems. I came up with what seems to me to be a working solution,...
12
votes
3answers
2k views
Worded Mastermind game in Python
Just completed a /r/dailyprogrammer challenge. Challenge #238 to be precise.
A Github repository with the wordbank.txt is available here.
...
12
votes
2answers
931 views
Project Euler #1 Sum of multiples of 3 and 5 python implementation
Given:
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 3 or 5 below 1000.
...
12
votes
4answers
2k views
Reverse Polish Notation (RPN) Calculator
I am trying to solve a codewar challenge (link):
Your job is to create a calculator which evaluates expressions in Reverse Polish notation.
For example expression ...
12
votes
1answer
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. ...
12
votes
2answers
3k 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 ...
11
votes
4answers
14k views
Python 3 function to rotate an image 90°
A coding challenge that rotates an image 90 degrees counterclockwise. The image is represented as an array matrix. I believe the time complexity is O(n2), but I'd like to know for sure, as well as any ...
11
votes
9answers
64k views
Find the repeated elements in a list
You are given an array of n+2 elements. All elements of the array are in range 1 to n. All elements occur once except two numbers, which occur twice. Your task is to find the two repeating numbers.
...
11
votes
6answers
3k views
Transform letters in a string
I am learning python and want to improve my skills in it. So could someone advice of how to improve the code below (this is a simple programming problem taken from one of the web sites). I feel like ...
11
votes
5answers
815 views
Reversing a number
I was working through a programming challenge I found on Reddit and it seemed easy enough:
Find all numbers less than \$10^n\$ where the number and its reverse (the reverse of \$123\$ is \$321\$) ...