All Questions
Tagged with programming-challenge python
1,025 questions
9
votes
1
answer
2k
views
Codility MaxCounters peformance in Python capping out at 77% performance
My problem is quite similar to the question found here, except I am attempting to answer the question in Python.
Given an array of N counters, all initialized to 0, and an array ...
0
votes
3
answers
201
views
My take at ProjectEuler #17 - counting counting characters
I tried to tackle #17 problem on Project Euler:
If the numbers 1 to 5 are written out in words:
one, two, three, four, five,
then there are \$3 + 3 + 5 + ...
4
votes
1
answer
659
views
Sum of numbers which are not sum of two abundant numbers
The following program is a solution to Project Euler Problem 23. That problem defines an abundant number to be one whose sum of proper divisors is greater than it, tells us that all integers at least ...
4
votes
2
answers
148
views
Summing all parameters, except the last, which should be subtracted instead
Working on some python koans and got one which states:
...
4
votes
5
answers
3k
views
Finding if a given number is in a given range based on a flag
Question Description:
Given a number n, return True if n is in the range 1..10, inclusive. ...
4
votes
1
answer
5k
views
Skyline problem solved by sweep line
The Skyline problem from UVa Online Judge is as follows:
You are to design a program to assist an architect in drawing the skyline of a city given the locations of the buildings in the city. A ...
2
votes
3
answers
377
views
Optimizing run time of Python solution to Project Euler Problem 34
This is a brute force solution to Project Euler Problem 34 with only 1 optimization (i.e.: caching factorials):
...
2
votes
1
answer
2k
views
Index of letter removed to make string palindrome
This code finds the index in the string and removes the one which will make the string a palindrome. If the string is already a palindrome, it should return -1.
This code works correct, but I am ...
9
votes
1
answer
620
views
Horridly Inefficient Project Euler #14 in Python - Collatz Sequences
I created this solution a while ago, and have been going back over my problems to try to make them all more efficient. I have been stuck on this one, and have tried a few different ideas. So far, my ...
3
votes
1
answer
2k
views
Performance in Hackerrank challenge "Sherlock and Queries"
I translated the solution by the author to his challenge from C++ to Python.
It seems to be correct, but I get timeouts for the test cases 6, 9 and 12.
Here is the full challenge description:
...
3
votes
2
answers
208
views
Answer to an implementation of Project Euler #8
I saw that there was a previous question asking to review an answer to the Project Euler question #8. My answer didn't look anything like the answer in that question and as a beginner, I was wondering ...
8
votes
1
answer
7k
views
Functional Knapsack Problem in Python
This is the knapsack problem from rosettacode.org:
A tourist wants to make a good trip at the weekend with his friends. They will go to the mountains to see the wonders of nature, so he needs to ...
2
votes
2
answers
144
views
Optimizing code for sequence A064604
I am implementing A064604 - OEIS for positive integers up to 10 billion.
I am finding the divisors in \$O(\sqrt N)\$. So, the overall time complexity of running the formula right now is \$O(N\sqrt N)\...
5
votes
1
answer
1k
views
Project Euler #10 - summation of primes
(From Project Euler)
The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.
Find the sum of all the primes below two million.
Can someone please help me improve this code?
...
6
votes
3
answers
3k
views
Caesar cipher exercise
I was doing this exercise from here. I made a solution work but can't help feel it could have been done in fewer lines. I'm quite new to python so I'm not too sure of the little shortcuts.
...
5
votes
2
answers
4k
views
HackerRank "Manasa and Stones" in Python
Problem Statement:
Manasa is out on a hike with friends. She finds a trail of stones with numbers on them. She starts following the trail and notices that two consecutive stones have a difference of ...
9
votes
3
answers
4k
views
HackerRank "Service Lane" in Python
Problem statement:
Calvin is driving his favorite vehicle on the 101 freeway. He notices that the check engine light of his vehicle is on, and he wants to service it immediately to avoid any risks. ...
4
votes
3
answers
3k
views
Project Euler 81 (minimum path sum through a matrix)
Problem Statement:
In the 5 by 5 matrix below,
131 673 234 103 18
201 96 342 965 150
630 803 746 422 111
537 699 497 121 956
805 732 524 37 331
...
13
votes
1
answer
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 ...
9
votes
3
answers
11k
views
Project Euler #9 - Pythagorean triplets
This code works, and I'm trying to figure out if there's a better way to optimize it. As you can see, I've cut down drastically on time just by using numbers that work for ...
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, ...
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 = ...
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.
...
11
votes
3
answers
37k
views
Sum of Squares/Square of Sum Difference
I am doing Project Euler problem #6. This is the question it asks:
The sum of the squares of the first ten natural numbers is,
$$1^2 + 2^2 + \ldots + 10^2 = 385$$
The square of the sum of ...
3
votes
2
answers
1k
views
Efficiency of Project Euler 28 - number spiral diagonals
I have written a solution for Problem 28.
Starting with the number 1 and moving to the right in a clockwise direction a 5 by 5 spiral is formed as follows:
$$\begin{array}{rrrrr}
\color{red}{...
7
votes
3
answers
19k
views
Codility Frog Jump solutions [closed]
Problem details from Codility:
A small frog wants to get to the other side of the road. The frog is currently located at position X and wants to get to a position greater than or equal to Y. The ...
2
votes
1
answer
149
views
Painting Fences - optimize for faster performance
I am attempting this problem at Codeforces. I'm stuck as it gives a time limit exceeded error for large inputs though the logic I followed is similar to that of the editorial.
I'd be grateful for any ...
7
votes
6
answers
5k
views
Improving efficiency for two sum
I am trying to solve this problem:
Given an array of integers, find two numbers such that they add up to a specific target number.
and this is my strategy:
For each ...
1
vote
2
answers
1k
views
Application of higher-order function in Python
If \$f\$ is a numerical function and \$n\$ is a positive integer, then
we can form the \$n\$th repeated application of \$f\$, which is
defined to be the function whose value at \$x\$ is
\$f(f(......
3
votes
1
answer
291
views
Speeding up Codechef FROGV
This is my solution to the FROGV problem on Codechef:
...
-2
votes
1
answer
9k
views
hailstone sequence using recursion in python [closed]
Douglas Hofstadter's Pulitzer-prize-winning book, Gödel, Escher, Bach, poses the following mathematical puzzle:
Pick a positive integer n as the start.
If n is even, divide it by 2.
If n is ...
5
votes
2
answers
2k
views
Minimum number of letters removed, to make a string Peragram
Here is the link to the problem Peragrams.
Peragrams: If a word is an anagram of at least one palindrome, we call it a Peragram.
Problem: Given a string, find the minimum number of letters ...
3
votes
1
answer
514
views
Typeahead Talent Buddy
Problem is Talent Buddy.
Your task is to
write a function that prints to the standard output (stdout) for each query the user name that matches the query
if there are multiple user names ...
5
votes
3
answers
729
views
Tweets per second
This challenge is from Talent Buddy:
Tweets per second
Japan Castle in the Sky airing broke a Twitter
record on August 3, 2013. At one point during the classic anime movie
showing, people ...
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 ...
16
votes
6
answers
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 ...
7
votes
5
answers
2k
views
Improving efficiency of Project Euler 185 (16-place Mastermind)
This a solution for Project Euler Problem 185. It works fine for the example but slow for the problem statement. How can I improve its speed and efficiency?
...
4
votes
2
answers
375
views
Efficient use of regular expression and string manipulation
The following is my solution to Java vs C++. I think the way I have used the re library is inefficient, and possible erroneous as I am getting tle.
...
4
votes
3
answers
1k
views
Speeding up Project Euler 43 - sub-string divisibility
The number, 1406357289, is a 0 to 9 pandigital number because it is
made up of each of the digits 0 to 9 in some order, but it also has a
rather interesting sub-string divisibility property.
...
7
votes
7
answers
9k
views
Python address index +1 with list comprehension
Task from CodingBat:
Return the sum of the numbers in the array, returning 0 for an empty array. Except the number 13 is very unlucky, so it does not count and numbers that come immediately after a ...
2
votes
1
answer
310
views
Project Euler 40: Champernownes's Constant
I just finished Project Euler 40 with a brute force method. As usual, any optimizations would be helpful. (I feel like I'm getting better at this scripting thing)
...
3
votes
1
answer
983
views
Project Euler 39: Integer right triangles
I just finished Project Euler 39:
If p is the perimeter of a right angle triangle with integral length sides, {a, b, c} … For which value of p ≤ 1000, is the number of solutions maximised?
I'm ...
5
votes
3
answers
904
views
Project Euler 38: Pandigital Multiples
I just solved Project Euler 38 and was wondering if you guys could provide some suggestions on how to speed it up. I've tried adding extra conditions but they increase the time. Any ideas on how to ...
3
votes
1
answer
740
views
Truncatable Primes -- Project Euler 37?
I just finished Project Euler 37 after a bit of debugging and can't figure out how to make it faster. Whenever I add extra tests that I want to speed the program up, it ends up just slowing it down. ...
1
vote
3
answers
607
views
Optimizing Project Euler 36 - double-base palindromes
This one was quick and simple. Any optimizations?
...
2
votes
1
answer
161
views
Project Euler 35 in Python
I'm back with a Python solution for Project Euler 35. Any help would be appreciated; I generate the numbers by observing that all circular primes can only have digits 1, 3, 7, and 9. Then I use the ...
6
votes
2
answers
4k
views
Project Euler 34 - digit factorials
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\...
8
votes
1
answer
1k
views
Project Euler Problem 33 - digit canceling fractions
Problem:
The fraction \$(\dfrac{49}{98})\$ is a curious fraction, as an inexperienced
mathematician in attempting to simplify it may incorrectly believe
that \$(\dfrac{49}{98}) = (\dfrac{4}{8}...
4
votes
2
answers
1k
views
How to make Project Euler 32 in Python faster?
We shall say that an n-digit number is pandigital if it makes use of
all the digits 1 to n exactly once; for example, the 5-digit number,
15234, is 1 through 5 pandigital.
The product 7254 is ...
1
vote
2
answers
1k
views
Any way to optimize this already fast Python solution to Project Euler 31 (coin sums)?
This is a Python solution for all general cases. Any suggestions at all would be appreciated; in my opinion it's already quite good! Here's a link to the problem.
...