Write a method to replace all spaces in a string with '%20'. You may assume that the string
has sufficient space at the end to hold the additional characters, and that you are given the "true" length of the string.
This is how I tried to solve, but I know I haven't addressed the question correctly where author asks us to assume the additional space at the end and the true length of the string. I am a beginner in programming trying to learn Algorithms and DS, quite a head bagging on this side.
def replace(astring):
alist= list(astring)
for i in range(len(alist)):
if alist[i] == ' ':
alist[i]= '%20'
return (''.join(alist))
print(replace("Mr John Smith "))
Since you are trying to learn algorithms, I think it will be beneficial to leave two hints to help you forward instead of the answer
"Mr John Smith ".rstrip() will strip any space at the end of a string. This means you will not have to worry about any logic for whitespace
Even though the solution works, you are using extra space by creating alist. Maybe try replacing things in the string itself? In Python a string can already by accessed as a string. For example astring[:2] is valid. Also 'hello world!'.replace(' ', 'test') is your friend
You can use the following code :
import urllib.parse
urllib.parse.quote("Mr John Smith ")
Related
So i'm new to Python and i'm going through a Python course I purchased and they have a quiz. The last question was to print the last 6 letters of the string. The code is below:
welcome_message = "Hello and welcome to the land of Python"
print(f"The last 6 letters of the welcome message:\n'{welcome_message}'\nare: '{welcome_message[len(welcome_message)-6:]}'")
The output would then be:
The last 6 letters of the welcome message:
'Hello and welcome to the land of Python'
are: Python
This is from the solution. I am not understanding what's going on here: '{welcome_message[len(welcome_message)-6:]}'
I don't understand why the solution included the len() function.
Why can't I just do '{welcome_message[-6:]}'
?
You'll get the same output with this too.
In python -1 index is same as the last index and when its blank it means starting or ending depending on where you put it. for eg.
welcome_message[:]
will print the entire string.
As for your question you can use welcome_message[34:] which instead of counting yourself a better way of writing is welcome_message[len(welcome_message)-6:].
But an even better way of writing is the solution you pointed out, i.e,
welcome_message[-6:]
print(f"The last 6 letters of the welcome message:\n'{welcome_message}'\nare: '{welcome_message[len(welcome_message)-6:]}'")
Here is what is happening welcome_message is a variable which can have infinite letters/character/number/symbols/strings etc.. which the system does not know first hand...
So welcome_message[len...] first finds how many characters are there in the string, not words... I say characters because we supply len() function with the welcome_message variable which has just 1 string... so thus far i hope I explained what happened till
{welcome_message[len(welcome_message)]} and then its just plain old -6 arithmetic operation from the count that is returned by the len() fn
welcome_message = "Hello and welcome to the land of Python"
print(f"The last 6 letters of the welcome message:\n'{welcome_message}'\nare: '{welcome_message[len(welcome_message)-6:]}'")
Here welcome_message is storing a string which is "Hello and welcome to the land of Python".
while printing if we will add \n in a string it will output a newline in answer.
len(welcome_message)-6 = 39-6 = 33.
in string slicing s[i:] it will give output as a string which includes the character from i to end of the string.
Hence welcome_message[len(welcome_message)-6:] will output the characters from index of 33 to 39th index.
Remember that " " is also a character of the string.
Hey Guys Need Your Help Here.
This is my sample code
import random
import string
import re
str=r'im a boy \\x%02X'
result = re.sub('\\\\x%02X', re.escape('\x5c\x5c\x78\x25\x30\x32\x58'), str, 0)
print(" Code=\n", result)
So when i took output from this program. The output is:
Code=
im a boy \\x%02X
But it is supposed to be like this/
Code=
im a boy \x5c\x5c\x78\x25\x30\x32\x58
Why it is not replacing??
There's a number of mistakes in your code, which makes me wonder if r'im a boy \\x%02X' is actually a correct representation of your input.
Assuming it is:
import re
s = r'im a boy \\x%02X'
result = re.sub(r'\\\\x%02X', re.escape(r'\x5c\x5c\x78\x25\x30\x32\x58'), s)
print(result)
Some of the problems with your code:
Why is random in there? And string?
Don't name variables so that they shadow basic types (or any builtins), str is a bad name
Without the r the \x5c[etc] string will turn into the exact same string as you had before, defined as bytes.
Why use the byte representation of the exact same text as a the replacement for the text?
Why tell re.sub to only replace 0 times?
By the way, from what you appear to be trying here, I wonder if you realise that what you're printing and what the actual value of a variable is, isn't exactly the same thing. What you print is the text representation of the value, not the actual value.
You would do well to explain why you're attempting this replacement, as this may be a typical XY problem.
My program seems to be indexing the wrong character or not at all.
I wrote a basic calculator that allows expressions to be used. It works by having the user enter the expression, then turning it into a list, and indexing the first number at position 0 and then using try/except statements to index number2 and the operator. All this is in a while loop that is finished when the user enters done at the prompt.
The program seems to work fine if I type the expression like this "1+1" but if I add spaces "1 + 1" it cannot index it or it ends up indexing the operator if I do "1+1" followed by "1 + 1".
I have asked in a group chat before and someone told me to use tokenization instead of my method, but I want to understand why my program is not running properly before moving on to something else.
Here is my code:
https://hastebin.com/umabukotab.py
Thank you!
Strings are basically lists of characters. 1+1 contains three characters, whereas 1 + 1 contains five, because of the two added spaces. Thus, when you access the third character in this longer string, you're actually accessing the middle element.
Parsing input is often not easy, and certainly parsing arithmetic expressions can get tricky quite quickly. Removing spaces from the input, as suggested by #Sethroph is a viable solution, but will only go that far. If you all of a sudden need to support stuff like 1+2+3, it will still break.
Another solution would be to split your input on the operator. For example:
input = '1 + 2'
terms = input.split('+') # ['1 ', ' 2'] note the spaces
terms = map(int, terms) # [1, 2] since int() can handle leading/trailing whitespace
output = terms[0] + terms[1]
Still, although this can handle situations like 1 + 2 + 3, it will still break when there's multiple different operators involved, or there are parentheses (but that might be something you need not worry about, depending on how complex you want your calculator to be).
IMO, a better approach would indeed be to use tokenization. Personally, I'd use parser combinators, but that may be a bit overkill. For reference, here's an example calculator whose input is parsed using parsy, a parser combinator library for Python.
You could remove the spaces before processing the string by using replace().
Try adding in:
clean_input = hold_input.replace(" ", "")
just after you create hold_input.
I am a beginner in python. I came across this question in codewars.
Jaden is known for some of his philosophy that he delivers via Twitter. When writing on Twitter, he is known for almost always capitalizing every word.
Your task is to convert strings to how they would be written by Jaden Smith. The strings are actual quotes from Jaden Smith, but they are not capitalized in the same way he originally typed them.
Example :
Not Jaden-Cased: "How can mirrors be real if our eyes aren't real"
Jaden-Cased: "How Can Mirrors Be Real If Our Eyes Aren't Real"
This is my attempt (I am supposed to code using a function)
def toJadenCase(string):
l = len(string)
for i in range(0,l):
if string[i] == ' ':
y = string[i]
string[i+1] = chr(int(y)-32)
return srting
s = raw_input()
print toJadenCase(s)
When run, the following errors showed up
How can mirrors be real if our eyes aren't real (this is the input string)
Traceback (most recent call last):
File "jaden_smith.py", line 9, in <module>
print toJadenCase(s)
File "jaden_smith.py", line 6, in toJadenCase
string[i+1] = chr(int(y)-32)
ValueError: invalid literal for int() with base 10: ''
I couldn't understand these errors even after google-ing it. Any help would be appreciated. I would also be great if other errors in my code are highlighted and a better code is suggested.
Thanks in advance :D
As Goodies points out, string should not be used as a variable name
Following the Zen of Python, this is technically a function that does exactly what you're trying to achieve:
def toJadenCase(quote):
return quote.title()
Edit:
Revised version to deal with apostrophes:
import string
def toJadenCase(quote):
return string.capwords(quote)
First you have to understand that strings are immutable, so you cannot set a single character inside a string, but build a new string from the old one and replace the old one (this can be usually done still in one pass so it's not a big complication).
Second, for most of these kind of operations, it is much better to use the methods of the string object itself, rather than redo everything from scratch.
Said that, there is still some complication with the question, but a function that does what you want is in the module string:
import string
s="How can mirrors be real if our eyes aren't real"
newstring=string.capwords(s)
If you prefer (why?!) a DIY solution (using string methods):
newstring=' '.join([ss.capitalize() for ss in s.split()])
Note that using split without argument splits the string on any whitespace (e.g. tabs etc.), that I think is the desired behavior.
If you want to do this without using a function that already exists, this is how I would do it and I'll explain everything:
Assuming you get a string with ONLY text based words and all words start with a character*
def toJadenCase(string):
words = string.strip().split()
# This first strips all empty spaces around the words in the text and then splits the string by spaces (default) otherwise you can add a character inside split in order to split it at the character. This returns a list of words in the sentence.
li = [] # initialize empty list
for word in words:
word = chr(ord(word[0])-32) + word[1:]
# So there's a couple of things going on here.
# I could use .upper() to upper case something (like word[0].upper() + word[1:]
# in order to get it but I wanted to do it without the use of that.
# That being said, ord just figures out the ascii number and subtracting
# 32 makes it uppercase. chr changes it back to a string.
# Then it can be concatenated to the rest of the word.
# Strings can be treated as lists in python so word[0] and word[1:] works
Also, word[1:] just means from the 1st index to the end.
li.append(word) # this appends the word to the list
return ' '.join(li) # this joins all of the words in the list with a space
Now, if you want something a lot more concise (you can use .capitalize()):
def toJadenCaseShort(string):
return ' '.join([x.capitalize() for x in string.strip().split()])
which returns:
>>> abc("hello my friends")
'Hello My Friends'
Basically what it does is it uses list comprehension to strip and then split the words, capitalizes them, and then joins them with spaces!
Of course, you could just use string.title() as mark s. says but what's the fun in that? :)
Here is the answer that passed for me
import string
def toJadenCase(str):
quote = string.capwords(str)
return quote #Do not use print(quote) as it adds spaces
def toJadenCase(str):
quote = string.capwords(str)
return quote #Do not use print(quote) as it adds spaces
I'm trying to write code that parses a large text file. However, in order to get said text file, I run the original PDF file through pdfminer. While this works, it also returns my text file with many random spaces (see below)
SM ITH , JO HN , PHD
1234 S N O RT H AV E
Is there any easy way in Python to remove only certain spaces so words aren't separated? For the sample above, I want it to look like
SMITH, JOHN, PHD
1234 S NORTH AVE
Thanks.
Most likely what you're trying to do is impossible to do perfectly, and very hard to do well enough to satisfy you. I'll explain below.
But there's a good chance you shouldn't be doing it in the first place. pdfminer is highly configurable, and something like just specifying a smaller -M value will give you the text you wanted in the first place. You'll need to do a bit of trial and error, but if this works, it'll be far easier than trying to post-process things after the fact.
If you want to do this, you need to come up with a rule that determines which spaces are "random extra spaces" and which are real spaces before you can code that in Python. And I don't know that there is any such rule.
In your example, you can handle most of them by just turning multiple spaces into single spaces, and single spaces into nothing. It should be obvious how to do that. Even if you can't think of a clever solution, a triple replace works fine:
s = re.sub(r'\s\s+', r'<space>', s)
s = re.sub(r'\s', r'', s)
s = re.sub(r'<space>', r' ', s)
However, this rule isn't quite right, because in JO HN , PHD, the space after the comma isn't a random extra space, but it's not showing up as two or more spaces. And the same for the space in "1234 S". And, most likely, the same thing is true in lots of other cases for your real data.
A different somewhat close rule is that you only remove single spaces between letters. Again, if that works, it's easy to code. For example:
s = re.sub(r'(\w)\s(\w)', r'\1\2', s)
s = re.sub(r'\s+', r' ', s)
But now that leaves a space before the comma after SMITH and JOHN.
Maybe you need to put in a little information about English punctuation—strip the spaces around punctuation, then add back in the spaces after a comma or period, around quotes, etc.
Or… well, nobody but you can know what your data look like and figure it out.
If you can't come up with a good rule, the only option is to build some complicated heuristics around looking up possible words in a dictionary and guessing which one is more likely—which still won't get everything right (e.g., how do you know whether "B OO K M AR K" is "BOOK MARK" or "BOOKMARK"?), but it's the best you could possibly do.
What you are trying to do is impossible, e.g., should "DESK TOP" be "DESK TOP" or "DESKTOP"?