All Questions
Tagged with programming-challenge python
1,025 questions
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, ...
9
votes
1
answer
2k
views
LeetCode on Longest Palindromic Substring in Python
This is a programming question from LeetCode:
Given a string s, return the longest palindromic substring in s.
Example 1:
Input: s = "babad" Output: "bab" Note: "aba" is ...
5
votes
2
answers
366
views
Finding the number of moves a knight can perform while standing on a given square
This is a problem from CodeSignal which can be founded over here
Here is an image on what we have to do:
Code
...
8
votes
1
answer
974
views
Making an array of spiral numbers
Context
I was solving a task on CodeSignal here
You can view the task on the link I provided above.
I have also put the task at the bottom of the post.
Code
...
9
votes
3
answers
626
views
CSES standard solution for range query problem giving TLE
Problem statement is from CSES
This is a standard question where we are given a list of numbers and a number of queries. For each query, you have to give the sum of numbers in the given range.
My ...
4
votes
2
answers
2k
views
3-SAT Solver Python
I've written a 3-SAT solver based on this prompt:
Alice recently started to work for a hardware design company and as a part of her job, she needs to identify defects in fabricated integrated ...
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 ...
5
votes
2
answers
824
views
NEA Computing Task 2 Dice Game
I was recently set Task 2 as seen below and I realise someone answer the question on this site here but I wanted a fresh opinion
TASK 2:
Allows two players to enter their details, which are then ...
2
votes
0
answers
58
views
Getting the minimum possible value from list consist of '0' and '1' by multiply no. of [0,1] and add the result after sliced in 'k' section [closed]
This code write to determine a way to place the N horses into the K stables, so that the total coefficient of unhappiness is minimized.
I use python to write a code and I want to reduce the running ...
6
votes
1
answer
409
views
New Year Chaos (HackerRank problem) - find minimum swap count
The goal of the function is to find and print the minimum number of swaps (bribes) in what was initially a sorted array of ints - https://www.hackerrank.com/challenges/new-year-chaos/problem
I have ...
4
votes
1
answer
2k
views
Snake and ladder problem oops application
I am learning and trying to implement the best practice for snake and ladder game.
Rules of the game
The board will have 100 cells numbered from 1 to 100.
The game will have a six sided dice numbered ...
3
votes
1
answer
2k
views
LeetCode 1615: Maximal Network Rank
I'm posting a solution for LeetCode's "Maximal Network Rank". If you'd like to review, please do so. Thank you!
Problem
There is an infrastructure of n cities with some number of roads ...
3
votes
2
answers
5k
views
Beginner - OOP Project - Airline reservation system
Hi I just finished a VERY Basic airline reservation system. Wanted some feedback please let me know what you all think, if I am obfuscating information or passing parameters where I shouldn't or if ...
6
votes
2
answers
2k
views
Recreate Mario's half-pyramids using hashes (#) from CS50
I currently working on CS50. There is a problem where we need to recreate half-pyramids using hashes (#). The hashes are to substitute blocks like in Mario.
This is also the first time I'm using ...
4
votes
4
answers
1k
views
Check if a binary tree is symmetric in Python
I have solved the following Leetcode problem.
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For example, this binary tree [1,2,2,3,4,4,3] is symmetric....
1
vote
2
answers
369
views
Optimizing code in codewars
I was practicing python on codewars.com with this question:
Create a function that takes a positive integer and returns the next bigger number that can be formed by rearranging its digits. For example:...
7
votes
3
answers
581
views
Project Euler #645 -- speed up Monte-Carlo simulation in Python
I am trying to solve Q645. While the logic used for my code seems to be appropriate, the code itself is way too slow for the large number required in this question. May I ask for suggestion(s) to ...
3
votes
1
answer
46
views
Update UNIX-like system user profile picture with its GitHub account's one
Seeking to learning and have fun, I wrote out this script (what I expected to be) self explanatory and would appreciate some comments on that.
I published it as a gist and tried to do my best on what ...
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 ...
7
votes
2
answers
1k
views
This python code converts integers to strings and strings to ints without the inbuilt functions "int()" and "str()"
It is my solution from a challange from edabit.com named "Drunken Python". Thats the task:
You need to create two functions to substitute str() and int(). A function called int_to_str() that ...
2
votes
1
answer
179
views
LeetCode 839: Similar String Groups III
I'm posting my code for a LeetCode problem. If you'd like to review, please do so. Thank you!
Problem
Two strings X and Y are similar if we can swap two letters (in different positions) of X, so that ...
1
vote
1
answer
106
views
LeetCode 124: Binary Tree Maximum Path Sum 2
I'm posting my code for a LeetCode problem. If you'd like to review, please do so. Thank you for your time!
Problem
Given a non-empty binary tree, find the maximum path sum.
For this problem, a path ...
2
votes
0
answers
1k
views
Better performance in solution for the Dole Out Cadbury challenge
Dole Out Cadbury
Problem Description
You are a teacher in reputed school. During Celebration Day you were assigned a task to distribute Cadbury such that maximum children get the chocolate. You have ...
8
votes
1
answer
5k
views
XOR Encryption, Decryption, and Cracking in Python
I have recently been working through a number of the Cryptopals Cryptography Challenges to try and improve my knowledge and understanding of both cryptography and Python. As five of the first six ...
3
votes
1
answer
52
views
Box and Cox process
The code obtains Box and Cox transformed data. The original data is a Pandas DataFrame and the process uses this DataFrame.
I am trying to speed up the following code. If you can elaborate in faster ...
3
votes
1
answer
273
views
Analysis of multiple game scores
I am trying to create a list based on some data, but the code I am using is very slow when I run it on large data. So I suspect I am not using all of the Python power for this task. Is there a more ...
2
votes
0
answers
277
views
Performance challenge: Box operations (HackerRank) (C, Python)
Recently I've been doing some challenges on HackerRank and came across this one. First, I tried with Python, and then C. Both of my codes failed due to timeout restrictions.
It would be very helpful, ...
2
votes
2
answers
357
views
Exercism: determine if a word or phrase is an isogram
The task:
Determine if a word or phrase is an isogram.
An isogram (also known as a "nonpattern word") is a word or phrase without a repeating letter, however spaces and hyphens are allowed ...
0
votes
1
answer
467
views
LeetCode 1320: Minimum Distance to Type a Word Using Two Fingers II
I'm posting my Python code for LeetCode's 1320. If you have time and would like to review, please do so.
Problem
You have a keyboard layout as shown above in the XY plane, where each English ...
7
votes
1
answer
88
views
"Jedi's riddle" challenge
The challenge can be found here.
In short we have a recursive function, and we need to find when \$z_i\$ is equal to one.
$$
\begin{align}
z_1 &= 1\\
z_i &= z_{i-1} * a\ \%\ n\\
\...
3
votes
1
answer
412
views
LeetCode 146: LRU Cache II
I'm posting my code for a LeetCode problem. If you'd like to review, please do so.
Problem
Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following ...
9
votes
2
answers
3k
views
Alphabet Rangoli Challenge
The question is about printing alphabets in a rangoli pattern of size/dimension input by the user.
Example:
...
5
votes
1
answer
82
views
Finding all factors and prime factors of a number
I am working on the Project Euler, number 3 project (finding the largest prime factor of 600851475143), but I wanted to take it a step further and write something that will list all factors and prime ...
1
vote
1
answer
223
views
Exercism: Class representation of a matrix in Python
The task is to represent a Matrix as a class and to instantiate it from a string such as "1 2 3 4\n5 6 7 8\n9 8 7 6"
The class must provide methods for ...
0
votes
1
answer
633
views
LeetCode 76: Minimum Window Substring - Python
I'm posting a two-pointer, sliding-window problem of LeetCode (Minimum Window Substring) solved with Python. If you have time and would like to review the code, please do so, I appreciate that.
On ...
0
votes
5
answers
13k
views
LeetCode 1: Two Sum
This is an easy level LeetCode question, obviously not much code to review. I'm posting C++/Java/Python here. If you'd like to review, please do so. Thank you!
Problem
Given an array of integers, ...
1
vote
1
answer
211
views
Speeding up pandas apply
I would like to speed up pandas apply function. I have been using swifter . It currently takes about 5 mins for 200000 records using multiprocessing as below . Is there any way to speed this up ...
3
votes
2
answers
80
views
Using deque for composing a string from 2 alternating stacks
We have 2 strings, say "JACK" and "DANIELS". We create two stacks from these strings, and then loop over them comparing first elements: whichever of the 2 goes first in lexicographic order gets popped ...
2
votes
1
answer
297
views
Aho-Corasick algorithm to scan through a list of strings
This is a follow-up to my previous question about finding min and max values of an iterable.
Aho-Corasick algorithm was suggested to solve the problem. Below is my solution with the use of ahocorapy ...
2
votes
1
answer
72
views
Count "Word Order" with further optimization
Question:
You are given n words. Some words may repeat. For each word, print its number of occurrences. The output order should correspond with the input order of ...
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 ...
2
votes
1
answer
987
views
How to apply Python generators to finding min and max values?
I'm solving yet another problem in HackerRank (https://www.hackerrank.com/challenges/determining-dna-health/problem). In short: you are given 2 arrays (genes and <...
10
votes
4
answers
8k
views
Simple Sudoku Solver in Python
This is my naive approach on implementing a Sudoku solver, which is okay for simple Sudokus like this:
...
2
votes
1
answer
561
views
Calculate your total happiness, which depends on the integers in the given sets [closed]
I am a beginner in programming. And I am learning python as my first language. I went on to do some challenges on HackerRank and stumbled upon this problem. The problem goes like this:
There is an ...
8
votes
2
answers
537
views
Largest product in a grid - Project Euler 11
A stab at Project Euler Problem 11: Largest product in a grid
...
6
votes
2
answers
689
views
Euler 12: counting divisors of a triangle number
This is the question:
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 + 7 = 28. The first ten terms would be:
...
1
vote
1
answer
254
views
Maximal Char Requests in a String
I have been trying to deal with this problem set, and I have figured out how to solve it as well. But there is one problem that is ...
0
votes
1
answer
179
views
A python Hang Man Game [closed]
can anyone give me advice on how i can make my code more compact and efficient, and if you can, an implementation using OOP? Since, i just started learning Object Oriented Programming and it looks ...
0
votes
1
answer
82
views
Python User Input [closed]
I have recently completed the code challenge for exercise 48 in Learn Python3 the Hard Way. I was looking for any feedback on my solution and any ideas for improvement!
The full task can be found ...
8
votes
3
answers
281
views
Refactor the code which performs "cross-product", "reduce", "product of list" and "sum of list"
I have come up with a sequence of steps to find the maximum product of y positive numbers which add up to x. But the program is ...