All Questions
Tagged with strings python-3.x
137 questions
3
votes
1
answer
92
views
Multiline string concatenation
Problem Description
Printing the result of adding two numpy arrays together is very ugly.
Here we will use these numpy arrays as an example:
...
3
votes
2
answers
151
views
A simple search-and-replace algorithm
In recent times, I've been using the following algorithm repeatedly in different languages (Java, Python, ...) to search and replace strings. I don't like this implementation very much, it's very ...
1
vote
1
answer
211
views
Function that finds the exact number of times a string appears in a text
So I was tasked with providing a function that returns how many exact occurrences of attribute word appear in attribute text. The goal is for the function to be as fast as possible, and I managed to ...
2
votes
3
answers
137
views
Automate the boring stuff with python - Comma Code v2
This is the second version of code reviewed earlier.
Comma Code
Say you have a list value like this:
spam = ['apples', 'bananas', 'tofu', 'cats']
Write a function ...
1
vote
3
answers
196
views
Automate the boring stuff with python - Comma Code
Comma Code
Say you have a list value like this:
spam = ['apples', 'bananas', 'tofu', 'cats']
Write a function that takes a list value as an argument and returns a
...
4
votes
4
answers
2k
views
Matching words from a text with a big list of keywords in Python
I implemented the following code to retrieve medication names from a given text (prescription_text in the code). It works by matching words in the text with a list ...
7
votes
4
answers
432
views
Separating a String of Text into Separate Words in Python
Occasionally, we want to do a rudimentary parsing on English text; we separate the text into separate words.
...
3
votes
2
answers
434
views
Add two numbers represented as digit strings
This algorithm adds two numbers digit by digit using their string representations.
I am wondering what the time and space complexities of the following algorithm are. In particular, I am wondering ...
0
votes
2
answers
70
views
Reversing a Python string using [::-1] [closed]
I'm learning how to reverse list and strings. Is there another way of reversing a string other than using [::-1] method? even if the other method is the long route i would much appreciate it
...
1
vote
0
answers
77
views
Application for "Good" and "Bad" word checking
This thread is a continue version of Find match words based on a list of words where I now have added Good word dictionary. What I am trying achieve here is that I want to check if a user has a given ...
1
vote
1
answer
865
views
Adding string to a text file without duplicates
I have a function below, store_in_matchid_file, and it adds certain strings to a .txt file as long as that string doesn't already exist in the file. However that ...
2
votes
1
answer
188
views
follow-up - Checking Nested Bracket Levels in Strings Programming Challenge
A follow-up to this question, this post improves on test case issues.
To restate problem parameters, a given string S is considered closed if it:
Has a matching ...
3
votes
1
answer
1k
views
Python function to output a string that is justified to the right
I just solved this exercise from Think Python, 2nd edition:
Python provides a built-in function called len that returns the length of a string, so the value of len('allen') is 5. Write a function ...
4
votes
1
answer
585
views
Lingo (word-game) guessing strategy
My friend and I are kind of noobs in Python and are looking for ways to improve. We thought it would be cool if we could get some feedback on the following code. It is for a game named "Lingo&...
1
vote
2
answers
140
views
15
votes
5
answers
3k
views
Comma Code, Project from "Automate the Boring Stuff with Python"
I'm a newbie. I'm at Chapter 4 of Automate the Boring Stuff with Python.
Comma Code
Say you have a list value like this:
...
6
votes
2
answers
652
views
Flatten JSON to string
The code below is a Python module to flatten JSON-like structure (nested dictionaries and lists) to a single string. It also provides partial result in the form of flat list of strings. This ...
20
votes
6
answers
5k
views
Password validator
I have made a password verification "program" which takes user input and checks whether the password is valid or not. Based on that, a reply is printed.
...
3
votes
2
answers
4k
views
Separate an email address into its username and domain name
This is my first ever Python program and I wanted to get everyone's opinions on it. The goal of the program is to separate an email address into its username and domain name.
...
4
votes
2
answers
196
views
Retrieving top-level opening and closing sequences from a Python string
The problem I'm solving is a more complex version of pairing up opening and closing brackets.
Instead of matching only on ([{}]), I additionally need to match ...
1
vote
1
answer
535
views
Finding longest word in a sentence
A problem from Coderbyte. Find the longest word in a sentence after removing the punctuations and if two or more words are of same length return the first word
Below is my solution:
...
5
votes
3
answers
657
views
Generate random string to match the goal string with minimum iteration and control statements
The following is my python code to find the number of iterations needed to generate random string iteratively to match the input string.
Here I have repeated a similar kind of for loop in both the ...
5
votes
1
answer
1k
views
Capitalizing the first letter of every word in a string (with arbitrary spacing)
I solved the following question
You are asked to ensure that the first and last names of people begin
with a capital letter in their passports. For example, alison heck
should be capitalised ...
22
votes
6
answers
10k
views
Add 'ing' or 'ly' suffix to a string
I have been practising Python Practice questions and came across this question:
Write a Python program to add 'ing' at the end of a given string
(length should be at least 3).
If the given string ...
4
votes
1
answer
4k
views
Finding Pattern Score in Python
I saw this problem in C++ on here and decided to try it in Python, which was much simpler. I've used the same problem blurb as in the link above, so they are consistent. I'm sure my code can be ...
6
votes
2
answers
3k
views
Python - use a list of names to find exact match in pandas column containing emails
I have an Excel file containing a free-form text column (that sometimes is structured like an email), where I need to find all first and last names and add an extra columns saying TRUE/FALSE to these ...
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 ...
8
votes
2
answers
479
views
Pretty-print dollar amounts using custom precision for large amounts
Given a floating-point number, which is representing a dollar value, I'm aiming to return a rounded, human-readable string. Here are the rules for numbers equal to or greater than 1 million, equal to ...
-3
votes
1
answer
52
views
How to convert Recursive Approach to iterative one(Super Reduced String)? [closed]
The Question:
Steve has a string of lowercase characters in range ascii[‘a’..’z’]. He wants to reduce the string to its shortest length by doing a series of operations. In each operation he selects a ...
9
votes
2
answers
389
views
Navigate objects from a path provided as a string
Recently I wrote a validator for some objects. To increase usability the validator gives the user hints on how to fix some of the found issues. Since the application is around something programmatic, ...
3
votes
1
answer
2k
views
Find and return the earliest occurring word from a string which contains the most repeating letters
I'd like feedback on my solution to the outlined programming challenge (medium level). I've tried it fast in any way I know how to, but, what might be a more efficient and/or pythonic solution?
[...
3
votes
1
answer
3k
views
Run length encoding of an input string (Coderbyte 'Run Length' challenge)
I'd like feedback on my solution to the outlined (medium level) programming challenge. What might be a more efficient or Pythonic solution?
The challenge as outlined by Coderbyte:
[Run Length] (...
1
vote
1
answer
72
views
Spotting prime numbers with use of limited prime numbers
I'm looking for general feedback as how to improve my code. I wrote the program starting out with no functions, making it very difficult to adapt. When I separated the program into functions it became ...
5
votes
2
answers
2k
views
String replacement with dictionaries -- follow up
I was reminded by an achievement notification yesterday of this question of mine, posted nearly four and a half years ago. After reading through my own code and the review posted, I decided to re-...
8
votes
3
answers
3k
views
Generating a random string of 6 characters long in Python 3.6.2
I am learning Python, so pardon me if this is the very crude approach.
I am trying to generate a random string using alphanumeric characters.
Following is my code:
...
2
votes
1
answer
409
views
Simple Email Subject Filtering (python)
This challenge took me forever. I spent a lot of time messing with regular expressions, but couldn't get it to work. I ended up coming up with this abomination. If anybody wants to take the time I'...
2
votes
1
answer
156
views
Guessing The Word
Made after seeing A Simple Word Guessing Game. Any code critique is welcome!
...
1
vote
0
answers
128
views
How do I optimize memoization in order to find longest palindromic substring?
I want to find the longest palindromic substring using dynamic programming in Python3. The strings can be as large as this string.
I have seen other questions on this problem that successfully solve ...
11
votes
4
answers
4k
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-...
12
votes
5
answers
2k
views
Comma Code - Automate the Boring Stuff with Python
Below is my code for the Comma Code problem from Chapter 4 of Automate the Boring Stuff with Python. Is there anything I can do to make it cleaner?
Comma Code
Say you have a list value like this:
...
6
votes
2
answers
1k
views
Writing 1.5 million rows in a file
I am generating SQL statements (of MySQL format) and writing into a file to create 10,000 tables. Each table contains 5 columns with 1.5 million rows of data in each column. I generated these data by ...
2
votes
0
answers
58
views
Find text between two strings in data [closed]
I download a page to get it's source code. I then extract the first segment of the page that is between two strings that appear in the source code more than once.
How can my code be improved?
...
4
votes
2
answers
136
views
Converting a set into a string
The input is a set of alphabets.
The output is a string of 32 in length.
For any letter in the set if it is preceded by a '¬' we replace the n-th character in our output string with '0', such that n ...
0
votes
1
answer
60
views
String manipulation in Python
I have this very ugly piece of code which runs ethtool and then parses the output (Lilex is just a static class encapsulating subprocess and is not relevant to the ...
3
votes
2
answers
1k
views
Can this code, to convert string to integer, be made more compact?
Problem:
String to integer(leetcode)
Implement atoi which converts a string to an integer.
The function first discards as many whitespace characters as necessary ...
6
votes
1
answer
157
views
Search for strings in a list that have > 90% similarity
Explanation:
I am working on String comparison in which I want to cross-compare the list of the same input string.
For the same string, I am continuing the loop but if a single string will have ...
1
vote
1
answer
121
views
Short function to remove unnecessary whitespace
I have a function consisting of one line of code:
def trimString(string):
""" Remove unnecessary whitespace. """
return re.sub('\s+', ' ', string).strip()
...
4
votes
2
answers
175
views
String validation based on multiple criteria
I had me a code challenge and I was thinking: is there a way to make this more efficient and short? The challenge requires me to scan a string and ...
In the first line, print ...
1
vote
1
answer
5k
views
String Rotation
I have solved a previous year question of 2018 codevita (link) in Python.
Problem Description:
Rotate a given String in the specified direction by specified magnitude.
After each rotation make a ...
8
votes
2
answers
329
views
Sentence editor
I wrote a simple sentence editor which capitalises first word, deletes redundant spaces and adds a dot in the end unless another sign of punctuation is already present. Please help me to improve my ...