All Questions
Tagged with programming-challenge performance
629 questions
3
votes
1
answer
135
views
Efficient polynomial multiplication in Python
I'm practicing problems for the ICPC competition, and one of the problems requires solving it by using an FFT to compute the product of two polynomials efficiently. Since this is for the ICPC ...
1
vote
1
answer
96
views
Finding the number of distinct decompositions a number has using only repdigits
This is a problem from a previous semester that I am trying to upsolve. I am trying to solve a problem involving the total number of ways of decomposing a number using only repdigits. A repdigit is a ...
5
votes
3
answers
867
views
Project Euler 127 - abc-hits
Problem (Rephrased from here):
The radical of \$n\$, \$rad(n)\$, is the product of distinct prime factors of \$n\$. For example, \$504 = 2^3 × 3^2 × 7\$, so \$rad(504) = 2 × 3 × 7 = 42\$.
We shall ...
3
votes
1
answer
231
views
Leetcode: Number of Islands - BFS (Queue vs Recursion)
I was playing around with leetcode's Number of Islands.
As per the challenge's description:
Given an m x n 2D binary grid grid which represents a map of '1's
(land) and '0's (water), return the ...
3
votes
2
answers
94
views
Optimizing a Function to Check Pronic Numbers in JavaScript
I've written a function in JavaScript to check whether a given number is a Pronic number. A Pronic number, also known as an oblong number, rectangular number, or ...
4
votes
2
answers
63
views
Optimizing a Function to Generate a Row of Consecutive Odd Numbers in a Triangle
I've written a JavaScript function that generates a row of consecutive odd numbers in a triangle. The triangle looks like this:
...
3
votes
1
answer
106
views
Coding puzzle - Passing year books
Question:
There are n students, numbered from 1 to n, each with their own
yearbook. They would like to pass their yearbooks around and get them
signed by other students.
You're given a list of n ...
5
votes
1
answer
350
views
Leetcode: Largest Number
I was trying out leetcode's Largest Number.
As per the challenge's description:
Given a list of non-negative integers nums, arrange them such that
they form the largest number and return it. Since ...
4
votes
1
answer
145
views
Longest spell to cast from pages of spellbook follow-up
This question is from the PCTC 2022 R2 Past Paper and is a follow-up on my previous question. Previous question
I have implemented several solutions suggested, such as creating an array with pages ...
4
votes
2
answers
505
views
Leetcode: Steps to Make Array Non-decreasing
I was trying out leetcode's Steps to Make Array Non-decreasing
As per the challenge's description:
You are given a 0-indexed integer array nums. In one step, remove all
elements nums[i] where nums[i ...
1
vote
2
answers
127
views
Optimizing an algebraic assignment puzzle
I am going to do a math competition which allows writing programs to solve problems.
The code attempts to solve problem 25 from the Purple Comet Spring 2004 Mathematics Meet:
In the addition problem
<...
9
votes
4
answers
2k
views
Leetcode : First Missing Positive
I was trying out leetcode's first missing positive.
As per the challenge's description:
Given an unsorted integer array nums, return the smallest missing
positive integer.
You must implement an ...
4
votes
2
answers
295
views
Count frequency of words and print them in sorted order
Objective:
Write a program to count the frequencies of unique words from standard input, then print them out with their frequencies, ordered most frequent first. For example, given this input:
...
1
vote
1
answer
133
views
Read millions of integers from stdin and process them in Python within 2 seconds
This is a competitive programming problem which has never been solved in Python within the time limit of 2 seconds. It is about Disjoint Set Union:
Source: Algorithms In Context #10: Disjoint Sets
...
3
votes
2
answers
308
views
Trailing Digits
https://www.acmicpc.net/problem/23204
I've solved a programming challenge where you need to count the occurrences of a specific digit at the end of the product of multiples of a given number within a ...
3
votes
1
answer
328
views
Project Euler 60: Prime Pair Sets
Project Euler Prime Pair Sets:
The primes 3, 7, 109, and 673 are quite remarkable. By taking any two primes and concatenating them in any order the result will always be prime.
For example, taking 7 ...
2
votes
2
answers
317
views
Given an array, remove zero or more elements to maximize the reduction where you add odd values and subtract even values
Here's a code challenge I got. (I could not solve the challenge, I ran out of time. I rephrased the challenge language and I am trying the challenge again for personal growth & computer science ...
4
votes
3
answers
845
views
Codewars: Multiplying Polynomials
I'm trying to solve the following Kata from Codewars in Python.
My code produces the desired result for all the tests, yet it is not optimal, so it takes too much time to accomplish the big tests.
<...
0
votes
1
answer
250
views
Python Logic Gate Circuit Simulator [closed]
I have recently become interested in simulating logic circuits because of a electronics class at school. I have created a functioning simulator for logic gate circuits in Python. My implementation ...
2
votes
3
answers
548
views
Recursive palindrome check
I'm trying to solve this which basically calls for a recursive palindrome check with some minor extra steps (Special characters and whitespace can be ignored). The test inputs' length can be 100000 ...
4
votes
3
answers
144
views
Simple photomosaic generator
I wrote an implementation of Robert Heaton's Programming Exercise for Advanced Beginners #4 (photomosaics) in C99 using the MagickCore library. The full code is as follows:
...
2
votes
1
answer
154
views
LeetCode#494 target sum (dart is slower than python)
I'm trying to solve a LeetCode problem target-sum, in two languages: Python & Dart. The difference of time taken is shocking for me"
Python took ~70ms while Dart took ~900ms !! Why does Dart ...
3
votes
2
answers
417
views
Euler Project: Sums of Digit Factorials
MY CODE:
...
4
votes
2
answers
202
views
Project Euler #51: "Prime digit replacements" in Rust
Problem: https://projecteuler.net/problem=51
"Find the smallest prime which, by replacing part of the number (not necessarily adjacent digits) with the same digit, is part of an eight prime ...
3
votes
2
answers
230
views
Leetcode 14. Longest Common Prefix beats only ~50% of C++ solutions
The task is to find the longest common prefix. While I had no trouble to find the solution, I'm interested in the statistics of my code:
Runtime: 7 ms, faster than 56.36% of C++ online submissions ...
4
votes
2
answers
328
views
Counting primes less than n in Python
I implemented (a refinement of) the Sieve of Eratosthenes for counting primes less than a given number n. This is a coding exercise from LeetCode. The class Solution...
1
vote
1
answer
213
views
Infix calculator programming challenge Kattis.com
I'm working on this C++ coding problem:
Input is a sequence of up to commands, one per line, ending at
end of file. Each command is a definition, a calculation, or a clear.
All tokens within a ...
2
votes
1
answer
251
views
Leetcode problem: minimum removal to make valid parentheses
I was working on this problem on leetcode
Question
Your task is to remove the minimum number of parentheses ( '(' or
')', in any positions ) so that the resulting parentheses string is
valid and ...
0
votes
1
answer
126
views
Distribute items over array in order to minimize the difference between min and max array values
I came across this problem in a programming challenge a few days ago. I came up with the implementation below, however it resulted in a "time limit exceeded" failure for a few of the test ...
2
votes
3
answers
299
views
Kattis Battle Simulation
I have a working program for a problem, but need to optimize it in a way where the processing time is less than 3 seconds. This is where I found the problem:
https://open.kattis.com/problems/...
1
vote
2
answers
144
views
Function wrapper that prints the result
(English)
I was recently challenged to make a simple program that does the following:
Function f accepts as a parameter a function g, which returns a value. Function f must return a function h which ...
2
votes
3
answers
334
views
567: Permutation in String
So I've been working on this for a few days actually. I implemented a solution to the LeetCode problem: 567. Permutation in String
The Problem
Given two strings s1(queryStr) and s2(sourceStr), return ...
0
votes
1
answer
95
views
Codewars Runtime Error
I am currently trying to solve the problem "Is my friend cheating?" on Codewars. The Text are the Details to the Problem:
A friend of mine takes the sequence of all numbers from 1 to n (...
3
votes
1
answer
258
views
Efficiently calculating perfect powers in Haskell
I'm trying to calculate perfect powers to solve this code wars challenge: https://www.codewars.com/kata/55f4e56315a375c1ed000159/haskell
The number 81 has a special property, a certain power of the ...
3
votes
0
answers
190
views
Advent of Code 2021, Day 3 in Haskell
This is a working solution to today's Advent of Code puzzle, written in Haskell. However, I feel like this solution is not optimal.
...
1
vote
1
answer
1k
views
Python IPv6 verifier, parser and converter
This is a simple Python 3 program that verifies whether a string is a valid IPv6 address or not, parses IPv6 address to its hexadecimal and decimal values, and converts an integer to IPv6 format.
I am ...
1
vote
0
answers
115
views
Python functions to serialize nested data structures human readably
See Serializing (nested) data structures in a human-readable format for more details.
In the last two days I have significantly improved my function, and wrote seven implementations of it.
I have ...
2
votes
3
answers
402
views
Leetcode First Unique Character in a String code optimisation
I was working on First Unique Character in a String
Question
Given a string s, find the first non-repeating character in it and return its index. If it does not exist, return -1.
Example 1:
...
3
votes
2
answers
286
views
Drug Analyzer challenge
What do I need help with?
I'm doing the challenge below, in which I need to increase code performance. It's currently at 63% and I need to increase it to at least 71% however I can't optimize more ...
1
vote
2
answers
153
views
Improving Project Euler code problem #10
So, I just finished Problem 10 on Project Euler, but it took about 3 to 4 minutes for my output to come out, due to bad coding, I guess, any suggestions on how could I optimize my code?
Question
The ...
1
vote
1
answer
205
views
Rock paper scissors coding assignment
I had a coding assignment to write a Rock Paper Scissors game. The main task is not to show a solution but rather to test the coding style. The rules are such:
Problem:
rock is "O", paper is ...
1
vote
0
answers
368
views
Codewars: Path Finder
Task
You are at position [0, 0] in maze NxN and you can only move in one of the four cardinal directions (i.e. North, East, South, West). Return true if you can reach position [N-1, N-1] or false ...
4
votes
2
answers
199
views
Bisection, linear recurrences and even Fibonacci numbers
So this code is yet another attempt at solving the second Project Euler problem to improve my handling of Python. The purpose of the code is to solve the problem below
Each new term in the Fibonacci ...
1
vote
2
answers
634
views
Leetcode kth largest element without using heaps
I was working on kth largest element problem on leetcode
Question
Given an integer array nums and an integer k, return the kth largest element in the array.
Note that it is the kth largest element in ...
1
vote
3
answers
219
views
Count prime numbers whose rotations are all prime
This code solves 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 ...
2
votes
2
answers
894
views
return the sum of the squares that is equal to n²
I'm struggling to solve this Kata. My code works but I can't pass because it's timing out. I have googled it and even tried other people's code but still doesn't pass.
Any suggestion on how can I make ...
3
votes
2
answers
686
views
Python 3 approximate pi using 9 algorithms
Note: this script requires gmpy2, if you are using Python 3.9.5 x64 on Windows 10 21H1 like me, then you need to download the pre-built ...
6
votes
2
answers
1k
views
Python Place +,-, nothing between 1, 2, …, 9 (in this order) whose sum is 100
So this is a programming challenge I saw:
Write a program that outputs all possibilities to put + or - or nothing between the numbers 1,2,…,9 (in this order) such that the result is 100. For example ...
1
vote
3
answers
388
views
Find the 10001st prime number
Project Euler 7: What is the 10 001st prime number?
I currently have this code:
...
3
votes
1
answer
539
views
Python script that converts Windows Registry Scripts (.reg) into PowerShell scripts (.ps1)
Well this a re-implementation of a PowerShell script that I wrote which does exactly the same thing, and I have ported it into Python.
After a quick Google search I found that there is only one other ...