Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

Python 

6.00.1x - Missed deadline, can someone correct my code for me please?

Missed the deadline to enter it in to be corrected, but still want to follow the course. Would someone mind correcting my work and telling me where I can improve on/be more efficient? Thanks!

Set 1:
Assume s is a string of lower case characters.

Write a program that counts up the number of vowels contained in the string s. Valid vowels are: 'a', 'e', 'i', 'o', and 'u'. For example, if s = 'azcbobobegghakl', your program should print:

Number of vowels: 5


My ans:
Spoiler (Click to View)

Set 2:
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


My ans:
Spoiler (Click to View)


Now, I struggeled a lot with the final one, and was wondering if anyone could give me a couple of hints on how to do it without fully telling me. Thanks in advance for the help!

Pastebin for answer 1/2: http://pastebin.com/yATSNcab

Final Question:
Assume s is a string of lower case characters.

Write a program that prints the longest substring of s in which the letters occur in alphabetical order. For example, if s = 'azcbobobegghakl', then your program should print

Longest substring in alphabetical order is: beggh
In the case of ties, print the first substring. For example, if s = 'abcbcd', then your program should print

Longest substring in alphabetical order is: abc
1 save all the Ors by putting as an array or using count()

Code
def num_of_vowels(word):
    vowels = ['a', 'e', 'i', 'o', 'u']
    total = 0

    for letter in word:
        if letter in vowels:
            total += 1

    return total

2 is ok since i think this is beginner course but you can also use regex or a counter

3 i will give you my idea but not the code. hold the 1st char then start looping from the 2nd, see if it's greater than the previous char (it is comparing ascii chart values) and if so add it to your sequence, if not then set the current to the 1st char to start again from there. check if the length of the current is bigger than the last result and if so, set it to the result
2) Your code is just fine but dont forget lower rule

PHP Code

<?php 
def num_of_bob
(word):
    return 
len([x for x in range(len(word)) if word[x:].lower().startswith('bob')])

    
print(
"Number of times bob occurs is:  {0}".format(num_of_bob("azcbobobegghakl"))) 


3)I have no idea but probably i did it .. ❓

PHP Code

<?php 
import string
import itertools


def 
final(word):
    
ret = []
    
prr = word.lower()
    
db = ["".join(x) for x in 
            itertools
.chain.from_iterable([list(itertools.combinations(prr,i))
              for 
i in range(1,len(prr)+1)])][::-1]
    
#Fix db variable to 1 line, i did it because fuck up page layout.
        
cache = ""
    
for word in db:
        for 
letter in word:
            
cache += letter
        
#print cache
        
back = test(cache)
        if 
back:
            
ret.append([[x for y in x][0] for x in back])
        
cache = ""
    
app = ""
    
for x in ret:
        
app += str(len(x[0]))+":"+x[0]+"\n"
    
biggest = "x"
    
for x in app.split("\n"):
        
#print word
        
if x[2:] in prr:
            if 
len(x[2:]) > len(biggest):
                 
biggest = x[2:]
     return 
biggest




def test
( PROBLEM):
    
counter = 0
    cache 
= False
    order 
= [x for x in string.ascii_lowercase]
    
_ = []
    
__ = []
    
word = ""
    
cac = False
    
for x in PROBLEM:
        if 
cache:
            
cac = cache
            word 
+= cache
        
if counter <= order.index(x):
            
word += x
            counter 
= order.index(x)
            
cache = False
        
else:
            
_.append(word)
            if 
not cac:
                
word = ""
            
cache = x
            counter 
= 0
            cac 
= False
            
continue
        if 
cac:
            
__.append(word)
    if 
__:
        return 
_+[__[-1]] 


//print final("azcbobobegghakl")
//beggh
//[Finished in 0.8s]

//print final("abcbcd")
//abc
//[Finished in 0.1s]


Can be buggy, can be messy but lol !


Possibly Related Threads...
Thread / Author Replies Views Last Post
[C] by kakarot007Nov 6, 2017 08:31 PM 6 231 Nov 6, 2017 08:31 PM
Last Post: HiddenEyE
[C#] by DeadLineNov 27, 2016 03:34 PM 30 2,955 Nov 27, 2016 03:34 PM
Last Post: sososerious
[Java] by StealthThraxNov 25, 2015 08:57 AM 0 102 Nov 25, 2015 08:57 AM
Last Post: StealthThrax