Can someone explain to me why I get empty string as a result? - python-3.x

What im trying to do is get the longest substring in s in which the letters occur in alphabetical order.
For some reason alphasub has no string in it at the end and I don't know why
start = 0
sub = 1
maxsub = 0
current = 0
s = 'azcbobobegghakl'
leng = len(s)
for i in range(leng):
if i != leng - 1:
if s[i] <= s[i+1]:
current = i
sub = 1
while current < (leng-1):
if s[current] <=s [current+1]:
sub += 1
current += 1
else:
break
if(sub>maxsub):
maxsub = sub
start = i
alphasub = s[start:maxsub]
print("longest substring is: " + alphasub)

String slicing takes starting and end position.
https://docs.python.org/2/tutorial/introduction.html
Change alphasub=s[start:maxsub] to alphasub=s[start:start+maxsub]. You should see the expected output.

It's good practice to use print's to check your code.
I've added some prints at the end of your code like so:
print(s)
print(start)
print(maxsub)
alphasub=s[start:maxsub]
print ("longest substring is: " + alphasub)
Which outputs:
azcbobobegghakl
7
5
longest substring is:
It is starting at 7, and ending at 5, which obviously doesn't work.

Related

I am trying to write a program that prints out the input string in reversed order from end to beginning

string = str(input("Please type in a string: "))
index = 0
while index < len(string):
print(string[index])
index += -1
I am getting an error that says (string index out of range)
strings in python are kinda like lists. You can use print(string[::-1]) to print backwards
Edit:
To print character by character
string = string[::-1]
index=0
while index < len(string):
print(string[index])
index += 1
The reason you are getting 'Index out of range' error is that your while condition will never be false since your index will almost always be less than 0.
This should fix that:
string = str(input())
index = len(string) - 1; # Start at the end of your string
while index > -1: # Until you have reached the end
print(string[index])
index -= 1 # Equivalent to 'index += -1' but cleaner

Sorting strings without methods and other types

Hello I have to reorder a string, I am banned from using other types and str methods
So my problem is that I could not figure out how to end my code to get it work with any string
I tried to compare the results with sorted() to check and I am stuck at the first exchange
My code:
i = 0
s1 = "hello"
s2 = sorted(s1)
while (i<len(s1)):
j=i+1
while (j<=len(s1)-1):
if (s1[i] > s1[j]):
s1 = s1[0:i] + s1[j] + s1[i]
j+=1
i+=1
print(s1)
print(s2)
I tried to add + s1[len(s1):] at the end of the operation but
I only had found the result for a single string(that I was testing) adding thisI am really stuck, how can I make it work for all the strings with different lenghts??
Thanks
You're not reconstructing the string correctly when doing s1 = s1[0:i] + s1[j] + s1[i] as you're replacing one character for the other but you omit to actually interchange the two and to add the remains of the splitted string to the end of the new string.
Given what your code looks like, I would do it like this:
i = 0
s1 = "hello"
s2 = sorted(s1)
while i < len(s1):
j = i + 1
while j <= len(s1)-1:
if s1[i] > s1[j]:
s1 = s1[0:i] + s1[j] + s1[i+1:j] + s1[i] + s1[j+1:len(s1)]
j += 1
i += 1
print("".join(s2))
# > 'ehllo'
print(s1)
# > 'ehllo'
Please tell me if anything is unclear!
I am banned from using other types and str methods
Based upon your criteria, your request is impossible. Just accessing the elements of a string requires string methods.
The technique that you are using is very convoluted, hard to read and is difficult to debug. Try running your code in a debugger.
Now given that you are allowed to convert a string to a list (which requires string methods), redesign your code to use simple, easy to understand statements.
The following code first converts the string into a list. Then loops thru the list starting at the beginning and compares each following character to the end. If any character is less then the current character, swap. As you step thru the string, the character swaps will result in a sorted list. At the end convert the list back to a string using join().
msg = 'hello'
s = list(msg)
for i in range(len(s) - 1):
for j in range(i + 1, len(s)):
if s[i] <= s[j]:
continue
# swap characters
s[i], s[j] = s[j], s[i]
print(msg)
print(''.join(s))

How to split a number into individual digits VB using substring

I tried to put seconds in 2 text-boxes, each digit in one. Example x= 56 x1= 5 and x2= 6
' s = TimeOfDay.Second
TextBox15.Text = s.Substring(0, 1)
TextBox16.Text = s.Substring(1, 1)'
When I try this I get the following error: System.ArgumentOutOfRangeException
Any ideas on how to fix this?
ArgumentOutOfRange exceptions occurs whenever you attempt to get a character that doesn't exist at the given position. So what is happening is that there is either not a String at position 0 with a length of 1 or there is not a String at position 1 with a length of 1.
To prevent this, add a simple If/Then statement to check if the length of the original String at least equal to the position of the character. Also for what it's worth, since you only want one letter, simply get the character at the desired index of the String.
Here is a quick example:
If s.Length >= 1 Then
TextBox15.Text = s(0).ToString()
End If
If s.Length >= 2 Then
TextBox16.Text = s(1).ToString()
End If
Fiddle: Live Demo
You don't need to convert it to a string before getting the digits, just doing the maths to get them will work well enough:
Dim rightNow = DateTime.Now
TextBox15.Text = (rightNow.Second \ 10).ToString()
TextBox16.Text = (rightNow.Second Mod 10).ToString()
And another approach.
Dim c() As Char = DateTime.Now.Second.ToString("00").ToArray
TextBox1.Text = c(0)
TextBox2.Text = c(1)

couting a set of word in python

Assume s is a string of lower case characters.
Write a program that prints the number of times the string 'bob' occurs in s. For example, if s = 'azcbobobegghakl', then your program should print
Number of times bob occurs is: 2
This is my answer, but i dont know what's wrong with my code. Please help
s = "azcbobobegghakl"
coutBob=0
i=0
for char in range (len(s)):
if char[i:i+3]=="bob":
coutBob+=1
else:
i=i+1
print ("Number of times bob occurs is: " + str(coutBob))
You need to subscript the string s, not the index:
for i in range(len(s)):
if s[i:i+3]=="bob":
coutBob+=1
I think this will help you.
b = list(s)
i = 0
j = 0
for i in range(0,len(b)-2):
if b[i]=='b' and b[i+1]=='o' and b[i+2]=='b':
j = j + 1
print ("Number of times bob occurs is: %d"%j)

Iterate through a string using split

I essentially have two list containing strings:
com = ['746365', '6365']
dec = ['6d955s2359d757bb40d0cf36bd7a35662d8b3']
I take the length of the first element in list a '746365' and cut list b into the same length as the first element, (len('746365') = 6) and the result of b is '6d955s'. I now wish to move along the element in b by one place until i reach the end ('62d8b3').
I currently have:
count = 0
for a in com:
for b in dec:
print(com.index(a), a)
length_of = len(a)
print(b[0 + count:length_of + count])
count = count + 1
This doesn't work. However if I remove the count parts and leave whats displayed beneath it works but I can't get it to move along by one
for a in com:
for b in dec:
print(com.index(a), a)
length_of = len(a)
print(b[0:length_of])
how can I adjust this code so that I print then move along by one and print the next?
The initial result should be:
6d955s d955s2 955s23 etc etc..
once the end of dec has been reached, it'll move onto the next value in com.
Thanks.
Try this:
for a in com:
for b in dec:
for i in range(0,len(b)-len(a)+1):
clipped_str = b[i:i+len(a)]
print(clipped_str)
Try this code:
start = 0
for c_element in com:
current_length = len(c_element)
end = start + current_length
for d_element in dec:
for _ in range(len(d_element) - current_length + 1):
print(d_element[start:end])
start += 1
end += 1
else:
start = 0

Resources