All Questions
Tagged with python-2.x strings
47 questions
26
votes
5
answers
110k
views
Replacing letters with numbers with its position in alphabet
If anything in the text isn't a letter, ignore it and don't return it.
a being 1, b ...
18
votes
2
answers
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 ...
11
votes
5
answers
7k
views
ASCII generator
Today I stumbled upon CodinGame, a site with programming challenges. I'll describe one of them.
ASCII art allows you to represent forms by using characters. To be precise, in our case, these forms ...
11
votes
1
answer
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.
...
10
votes
1
answer
9k
views
Given a string, find the length of the longest substring without repeating characters
I wrote an algorithm that solves this problem but I would love to get some feedback on it (especially since I'm not so confident in my Big \$O\$ skills).
Do you think this is a good quality algorithm?...
9
votes
2
answers
4k
views
Data encryption with python
I am just trying to solve one of the puzzle which is explained below:
The sentence if man was meant to stay on the ground god would have
given us roots after ...
8
votes
3
answers
2k
views
Replacing English words in a string with their Spanish equivalents by dictionary lookup
I want to improve my code's performance and running time, looking for write my loops better.
For example, I have a dictionary that contains words as keys, and their translation in Spanish as values.
...
8
votes
3
answers
4k
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?
...
8
votes
2
answers
4k
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
1
answer
220
views
A database of books, part 1
Backstory
I collect books. A lot of books. Whitepapers, (programming) cookbooks, transcripts, important letters, overviews, you name it. Usually in PDF, otherwise my house would be too small.
To keep ...
8
votes
1
answer
429
views
Hangman game, with the number of guesses depending on the word length
This is an interactive game between the computer and the player, where the computer chooses a word and the user has to guess the letters in that word.
The computer provides certain number of guesses ...
8
votes
1
answer
3k
views
Quoted string parser
I have written a string parser that is designed to split a string by spaces, excluding spaces wrapped in strings.
Here is some example inputs and outputs:
...
7
votes
4
answers
1k
views
Solving jumbled letters using a given dictionary
Given a dictionary, a method to do lookup in dictionary and a M x N board where every cell has one character. Find all possible words that can be formed by a sequence of adjacent characters.
Example:
...
7
votes
4
answers
7k
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 ...
7
votes
1
answer
134
views
Zalera calculator
I recently started replaying Final Fantasy XII. One of its optional bosses, Zalera, has, among others, the following mechanics:
His attack sequence is Lv. 2 Sleep, Lv. 3 Disable, Lv. 4 Break and Lv. ...
7
votes
1
answer
235
views
How random can you get?
I work for a service desk and change A LOT of passwords. Most the time the end user is a complete fool that doesn't understand the requirements for the password they want to use. So I created a ...
7
votes
3
answers
3k
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
2
answers
304
views
Find a famous writer in a random string
I'm doing a CodeEval challenge, where the object is to find a famous writers name and a year, inside of a random given string.
You have a set of rows with names of famous writers encoded inside. Each ...
6
votes
3
answers
2k
views
5
votes
2
answers
15k
views
Manipulating filenames using Python
I was tasked with creating a script to be able to rename some files and then move them into different folders based on a code in the filename.
...
5
votes
2
answers
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
1
answer
6k
views
Regular expressions - match only specified string length
I'm learning regex. Here I'm checking a string of type aA1 but want also to check it is only 3 characters long. Is there a better way of doing it please?
...
5
votes
2
answers
101
views
Readability in creation of a long output string
Below is a function from a driver that I wrote for and I2C temperature sensor.
The function takes as input the name of the bus and the device's bus address, reads it's status register, and then ...
5
votes
2
answers
2k
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
1
answer
848
views
Splitting URLs into hierarchy-level directories
The goal is to split a URL like http://q.com/a/b/c to:
['http://q.com/a', 'http://q.com/a/b', 'http://q.com/a/b/c']
My code:
...
5
votes
1
answer
111
views
Princeton KMP implementation
I implemented a Python version of KMP followed by Princeton Prof Robert Sedgewick's discussion.
However, I got a Time Limit Exceeds error. Per leetcode's explanation, TLE indicates that the solution ...
4
votes
4
answers
9k
views
Convert data into dictionary
I am trying to convert "Random = 1" into dictionary {"Random":1}
I tried
...
4
votes
2
answers
766
views
Word counter script
I made a word counter. It works as long as there aren't any lone punctuation marks. How could it be improved? (Could it be made simpler? Are the comments detailed/clear enough? etc.) I know it's ...
4
votes
3
answers
2k
views
Build a dictionary based on split string
I've written a simple script that takes a string filled with brackets and strings and builds them to a dictionary.
Example Input:
...
4
votes
4
answers
3k
views
Efficiently concatenate substrings of long list of strings
I am having performance problems with the following python function:
...
4
votes
2
answers
4k
views
Reversing substrings in parentheses
Problem:
The task is to reverse the strings contained in each pair of matching parentheses, starting from the innermost pair. The results string should not contain any parentheses.
Example:
...
4
votes
2
answers
1k
views
Finding palindromic strings of even length
Given a string of digits. Find the length of longest substring of even length i.e. 0,2,... which is a palindrome or which can be rearranged to form a palindrome (see example below).
Here is my code: ...
4
votes
2
answers
202
views
'Broken Record' Coding Challenge
I was asked to complete a coding challenge for an interview. The challenge being to write a test function to check whether or not a string was a broken record. A <...
4
votes
1
answer
292
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 -
...
4
votes
2
answers
721
views
Checking whether any anagram of a string appears within another string
I have the following code that takes a string t and checks to see if any anagram of t is a substring of ...
4
votes
1
answer
130
views
Python Dictionary Manipulation
I just finished working on a coding challenge for learning Python 2.7. Essentially, I'm a function that if it is fed a string such as:
...
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.
...
3
votes
3
answers
6k
views
Finding the longest palindromic substring
I have the following code that works correctly. I am wondering if there is a simpler way to go about implementing my solution and/or if there is anything that looks non-standard in my code. If anyone ...
3
votes
1
answer
311
views
A bitwise common prefix algorithm in Python
Is my algorithm slow because it has problems? Or, is any bitwise common prefix solution not going to give me the performance I'm looking for?
After profiling my algorithm, I found that over 60% of ...
2
votes
2
answers
985
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
2
answers
223
views
Computing the largest substring starting with a substring
Given a string and a non-empty substring sub, compute recursively the largest substring which starts and ends with sub and return its length.
...
2
votes
2
answers
5k
views
Count overlapping substrings
I'm looking for feedback on the actual logic of this simple Python 2.7 script. The automated grader passed it, but I expect it could be make more 'pythonic'. It was written for homework, and some of ...
1
vote
1
answer
995
views
The Substring Game! challenge
This is the Substring Game! challenge from HackerEarth:
Watson gives Sherlock a string (let's denote it by S). Watson calculates all the substrings of S in his favourite order.
According to ...
1
vote
2
answers
12k
views
Replacing data in a .csv file
The code works well and does what I intend for it to do. In essence, it opens a file referenced as 'resource'. This is a .csv file. I then searches for the keys in the dictionary and for each key that ...
1
vote
3
answers
52
views
`set_includes` (string subsequence-containment) in Python 2
I've implemented C++'s std::includes algorithm in Python, so that I can use it to efficiently implement a Scrabble "can I make this word" function:
...
0
votes
2
answers
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 ...
0
votes
1
answer
67
views
Dealing with a dirty table
I have to deal with a really ugly looking response I get back from a particular device. The string represents a table. Each section of the string represents the form:
...