All Questions

Tagged with
Filter by
Sorted by
Tagged with
80
votes
4answers
21k views

Function to print command-line usage for a program [closed]

The following function works well to print out a help message for the program I have written. It prints a multi-line string message that describes the command line usage with some examples: ...
10
votes
1answer
4k views

Z-Algorithm for pattern matching in strings

I was trying to refactor the following Python code (keeping the same time-complexity) which is an implementation of Z-Algorithm for pattern matching in strings. ...
7
votes
6answers
16k views

Determining if a word is an anagram of another

The question was to see if a word is an anagram of another word. How would you improve this? It returns True if an anagram, or False otherwise. ...
9
votes
3answers
472 views

n number of x on the y

Everyone knows "99 bottles of beer on the wall". Mat's Mug made a comment about mugs on the wall in The 2nd Monitor and I realized I never wrote a "beer on the wall" program. But that seemed way too ...
5
votes
4answers
5k views

Find the shortest whole repetitive substring

I'm working on a problem to find wholly repeated shortest substring of a given string, and if no match, return length of the string. My major idea is using a Trie ...
1
vote
2answers
6k views

replacing words with their abbreviations

I'm working on a program that aim to take sentences (currently in french) and compact them to a length of 38 characters while retaining as much information as possible. You can find another part of ...
0
votes
2answers
2k views

JSON string parsing

I need to parse a simple JSON string (flat JSON, no hierarchy) for keys and values, and there is a system constraint that I cannot use any built-in JSON library and can only read a string once due to ...
9
votes
1answer
5k views

Equation parser and solver

I wrote this equation parser and solver, I feel like it is well documented and tested: ...
5
votes
5answers
23k views

Reverse complement of a DNA string

I have a function in python where I want the reverse complement of a DNA string. This is what I have now: ...
5
votes
2answers
8k views

Print initial letter of an entered name

Just started learning python and I'm solving some beginner problems. I wrote a solution for a question without any edge cases like user not entering anything, entering numbers etc.: Question: Write ...
4
votes
1answer
527 views

Improving time complexity of finding the longest palindrome in Python

The Longest Palindromic Substring challenge from InterviewBit: Given a string S, find the longest palindromic substring in S. where a "substring" must be contiguous, and in case of ties ...
26
votes
4answers
2k views

Translating from English to Entean

I wrote a python script that converts English to Entean language from an anime called "The Devil is a part timer". I am sloppy at coding and know that the script can be made better. In the anime, the ...
23
votes
5answers
7k views

Comma Code (project from “Automate the Boring Stuff with Python”)

I'm currently learning Python using the book mentioned in the title. I just finished reading about lists, tuples, functions and methods, and one of the projects following this chapter is to create a ...
20
votes
2answers
7k views

String reversal in Python

This code will take as output any string (long, empty, with spaces...) and will return its reverse. Can I improve this algorithm so that it can be faster? Right now its complexity is \$O(n)\$. <...
15
votes
8answers
9k views

Python program to take in two strings and print the larger string

I have written a Python program to take in two strings and print the larger of the two strings. Here is my code - ...
10
votes
4answers
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 ...
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 ...
11
votes
3answers
2k views

Generating sequential alphanumeric values that match a certain pattern

I'm working on generating large volumes of test data for some performance testing. Part of this is going to involve generating a lot of values that represent an "MBI", which is a certain kind of alpha-...
10
votes
2answers
431 views

Naive implementation of KMP algorithm

After reading this answer to the question "High execution time to count overlapping substrings", I decided to implement the suggested Knuth-Morris-Pratt (KMP) algorithm. I used the pseudo-code listed ...
10
votes
2answers
253 views

Python implementation of find_all_indexes

I got asked this question before in an interview. For example, find_all_indexes('ababc', 'abc') returns 2. This Python function ...
6
votes
2answers
4k views

Find a best fuzzy match for a string

I am trying to find a best match for a name within a list of names. I came up with the below code, which works, but I feel that the creation of the intermediate ...
5
votes
3answers
1k views

LeetCode 65: Valid Number (Python)

Problem Validate if a given string can be interpreted as a decimal or scientific number. Some examples: ...
17
votes
2answers
1k views

Given a page of content, determine shortest snippet containing all search phrases (no order required)

A recruiter gave me a homework problem as a part of the recruiting process and after receiving my submission he told me that he decided not to proceed with me. When I asked for the reason, he told me ...
10
votes
1answer
19k views

String replacement using dictionaries

I've always been bothered by the fact that there weren't any built-in functions that could replace multiple substrings of a string in Python, so I created this function. Essentially, you supply it ...
6
votes
5answers
14k views

Longest substring in alphabetical order

The code is meant to find the longest substring that is in alphabetical order. I would like to know if I can do it more efficiently than I have done here. I am in my first week of my first ...
3
votes
2answers
342 views

Parsing JSON in one go

I need to parse a simple JSON string (flat JSON, no hierarchy) for keys and values, and there is a system constraint that I cannot use any built-in JSON library and can only read a string once due to ...
3
votes
3answers
211 views

English to Entean (V2) with maketrans

I wrote a python script that converts English to Entean language from an anime called "The Devil is a part timer". This is the second version of the script which was first posted here Translating ...
0
votes
2answers
2k views

Command line tool for extracting, searching, and converting

I just completed my first real application (command line app). It's simple, but I had no prior knowledge of Python. Everything was hit and miss, with many Python books and help from those in this ...
9
votes
2answers
7k views

URLify a string using Python

I have been working my way through the exercises in the book Cracking the Coding Interview. I wanted to get feedback for the following exercise. Write a method to replace all spaces in a string ...
8
votes
2answers
3k views

Finding words that rhyme

Preface I was trying to review this question on the same topic, but in the end many points I wanted to make were excellently explained by @ferada so I felt that posting my code and explaining the ...
8
votes
2answers
11k views

Finding the largest repeating substring

Here's my code that takes a large string, and searches for the longest reoccurring substring. It compares the first letter with every other until it finds a match, then saves it. Then it compares the ...
8
votes
3answers
3k views

Function to censor single word in a sentence

The following function censors a single word of choice in a sentence with asterisks, regardless of how many times the word appears. How can I further simplify this code? ...
7
votes
3answers
2k views

Finding the fastest common prefix of 2 strings in Python

I'm comparing my algorithms, slice_prefix and bit_prefix, with existing ones to find the common prefix length of 2 strings as ...
6
votes
1answer
76 views

Zalera calculator

I recently started replaying Final Fantasy XII. One of it's optional bosses, Zalera, has, among others, the following mechanics: His attack sequence is Lv. 2 Sleep, Lv. 3 Disable, Lv. 4 Break and Lv. ...
5
votes
2answers
3k views

Python string clean up function with optional args

I've got a function that I mainly use while web scraping. It gives me the ability to throw in a multi line address and clean it or a name field with unwanted characters and clean those, etc. Below ...
5
votes
2answers
1k views

Numbers to Text Program - Python Training

I have written a numbers to text challenge for some people in my organisation to practice their Python skills. I am an economist working with pandas, but I am trying to teach them some stuff about ...
5
votes
1answer
146 views

Text search in Python

Whilst self-studying algorithms, I came across the Karp-Rabin rolling hash search algorithm here. I decided to have a go at implementing it in Python: For ease of reading; the data-structure ...
5
votes
1answer
1k views

A recursive Boggle solver

`Boggle is a word game in which letters are randomly placed in a 4x4 grid e.g: A D Q P N L E M O S R T V K J H Words can be started from any letter and are ...
5
votes
0answers
275 views

Palindrome insertion challenge

Here is my solution to the following Daily Coding Problem challenge Given a string, find the palindrome that can be made by inserting the fewest number of characters as possible anywhere in the ...
5
votes
2answers
6k views

Testing whether a given string has unique characters

Here is my code which tests whether a given string has unique characters. The function is_unique which does a linear search for each character in the string is \$O(...
5
votes
3answers
4k views

Generating character permutations

I want to generate a 'dictionary' containing all 8 character permutations of upper-case letters such that the output file looks like: ...
4
votes
1answer
164 views

Remove determiners in a string

I'm working on a program that aim to take sentences (currently in french) and compact them to a length of 38 characters while retaining as much information as possible. You can find another part of ...
4
votes
1answer
3k views

Python name generator

This is a basic name generator I just wrote and want some feedback. It basically chooses between a vowel or a consonant and appends it to a string. Note that it never puts two vowels or two consonants ...
4
votes
1answer
318 views

Refactor Oxford Comma function (and other functions)

I wrote a program that managed projects and created a set of SQL files for each project. In the logic, I have a few functions that, each time I look at them, I wonder if they could be written in a ...
4
votes
1answer
217 views

High execution time to count overlapping substrings

I was doing this HackerRank problem which basically boils down to counting overlapping substrings in a string. I used this solution from StackOverflow to build this program - ...
3
votes
2answers
160 views

Simple evolutionary string matching

This is based on a very similar question I asked, found here. The intent of this program is to start with a set number of random strings of zeros and ones, and eventually evolve them to find one ...
3
votes
1answer
490 views

Knuth–Morris–Pratt string search algorithm

I finally think I've implemented the Knuth–Morris–Pratt string search algorithm correctly, now time for that lovely criticism that causes me to learn! The code doesn't look all that pretty however it ...
2
votes
2answers
921 views

Speeding up and fixing phone numbers from CSVs with Regex

I've hodgepodged together an attempt to extract all phone numbers from all CSVs in a directory, regardless of where they are and what format they're in. I want all phone numbers to be printed to a ...
2
votes
1answer
699 views

TDD: String Calculator Kata

String Calculator Create a simple String calculator with a method int Add(string numbers). The method can take 0, 1 or 2 numbers, and will return ...
2
votes
0answers
154 views

Not a very interesting implementation of a simple substitution cipher in Python

I know this has been done before, but I needed to brush up my Python skills. Also, I intend to publicly share this code on a blog post sometime in the near future. This is also an attempt to improve ...