d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Python 3.3 Problem > Return A String As Call Tree As Tuple
Add Reply New Topic New Poll
Member
Posts: 21,861
Joined: Dec 13 2010
Gold: 0.91
Nov 21 2013 07:42pm
Hey there,

I'm programming in Python 3.3

I got the task to define a function: call_word(s)

Arguments:

s - a string (can be a long text)



return shall be:

a tuple (wrd, ltr, rtr, n) that discribes the Call-tree of "call_word(s)"
also it shall be in an alphabetic order

wrd = word
ltr = left tree (words before wrd)
rtr = right tree (words after wrd)
n = the frequency of "wrd" in the String



e.g:

call_word("that is a test")

imo the result shall be: ("a", None,("is","test",("that","test",None,1),1),1),1)


I also got a function to split strings into a tuple after the first "space":

Code
alph = ["A","a","Ä","ä","B","b","C","c","D","d","E","e","F","f","G","g",
"H","h","I","i","J","j","K","k","L","l","M","m","N","n","O","o",
"Ö","ö","P","p","Q","q","R","r","S","s","T","t","U","u","Ü","ü",
"V","v","W","w","X","x","Y","y","Z","z"]

splint =[".", ",", "!", "?", ";", ":",]


def next_word(s):

""" split a string into a tuple with 2 elements

Arguments:

s -- the string you wanna split.

"""

## replace ß ##

s = s.replace("ß", "ss")


if s == None or s == ' ' or s == '':

return None

result = ''



while s[0] in alph:

result = result + s[0]
s = s[1:]

if s == '':
break



if s == '':
return (result, "")


if s[0] in splint:

result2 = s[2:]


elif s[0]== " " or s[0] == "\n":

result2 = s[1:]


elif s[0] == None:

return (result, result2)


else:

return None


return (result, result2)

######################### tests for next_word ##########################
# #
# print(next_word("hey, in your face")) #
# --> ('hey', 'in your face') #
# #
# print(next_word("wuhu, in your face")) #
# --> ('wuhu', 'in your face') #
# #
# print(next_word("I will punsh your face, in the face")) #
# --> ('I', 'will punsh your face, in the face') #
# #
# print(next_word("HEY, that's a test for the function next_word")) #
# --> ('HEY', "that's a test for the function next_word") #
# #
# print(next_word("test ey")) #
# --> ('test', 'ey') #
# #
# print(next_word("")) #
# --> None #
########################################################################





any ideas are welcome, pm me or post if you wanna help me,


This post was edited by Breee on Nov 21 2013 07:46pm
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll