0

The question is as follows:

"A string exists in CamelCase format. For example: "ThisIsACamelCaseString".

A procedure/function is required that will:

1) prompt for the original entry
2) separate each word of the string
3) store each word in a separate array/list element
4) fill unused array/list elements with a rogue string such as "(Empty)".

After processing the preceding example, the array contents will look like this:

This
Is
A
Camel
Case
String
(Empty)
(Empty)
(Empty)
(Empty)

You may assume that the original string will contain no more than 10 separate words. Write program code in Python for this design."

This is what I tried:

a = input("Enter: ")
lists = list(a)
len = len(a)
alpha = ["Empty"]*10
alpha[0] = lists[0]
for i in range(len):
    for j in range(len):
        if lists[j + 1].isupper():
            break
        alpha[i] = alpha[i] + lists[j + 1]

for i in range(10):
    print(alpha[i])

How do I find suitable code?

SkepticArt
  • 31
  • 6
  • It looks like you want us to write some code for you. While many users are willing to produce code for a coder in distress, they usually only help when the poster has already tried to solve the problem on his own. A good way to show this effort is to include the code you've written so far, example input (if there is any), the expected output, and the output you actually get (console output, tracebacks, etc.). The more detail you provide, the more answers you are likely to receive. Check the [FAQ](http://stackoverflow.com/tour) and [How to Ask](http://stackoverflow.com/help/how-to-ask). – Rory Daulton Apr 22 '17 at 16:05
  • 4
    your username is ironic especially when asking people to write code for you – gold_cy Apr 22 '17 at 16:07
  • @Rory Daulton I actually don't need the full code, but if any suggestion on how to separate the string into words as described above! Only suggestion would suffice or if I have to use any built-in functions! – SkepticArt Apr 22 '17 at 16:12
  • @Dmitry Polonskiy I don't teach people programming, it's just other things that I teach. I have that username in various other sites, and hence used it here too! Thank you, I mean no offense! :) – SkepticArt Apr 22 '17 at 16:14
  • Even if you do not want full code, you still need to show your own efforts. What ideas have you gotten, what have you tried, why is it not good enough? You really need to read the standards for this site. – Rory Daulton Apr 22 '17 at 16:29
  • @RoryDaulton I have just edited the question with what I have previously attempted, can you please help me now? Thanks in advance! :) – SkepticArt Apr 22 '17 at 18:27
  • I have now removed my close vote, due to your last edit, but since you have already accepted an answer I don't think you need another one from me or from anyone else. – Rory Daulton Apr 22 '17 at 18:31
  • @RoryDaulton That code just works fine, but it will be really great if you or anyone could answer the revised version of my question, but now it asks where I did it wrong, and previously it asked for a solution! :) – SkepticArt Apr 22 '17 at 18:39
  • I mean the person who answered my previous question has given a code that doesn't quite resemble mine and therefore I still can't spot where I did it wrong! I initially thought that someone else writing the code would help me spot my mistake, but it didn't! I hope you get my point! :) – SkepticArt Apr 22 '17 at 18:41
  • 1
    Since that answer really does answer your original question and you now want to also know something else, it would be best for you to re-accept that answer, edit this question back to know how to find suitable code (leaving in your attempted code), and *ask another question about what is wrong with your attempted code*. Improving a question is good here, but changing what you are asking is not. If you make it clear *this* question asks for a solution while your *new* question asks what is wrong with your attempt, you will not be posting a duplicate. – Rory Daulton Apr 22 '17 at 19:29
  • Okay, thank you, I will do that :) – SkepticArt Apr 22 '17 at 19:58

2 Answers2

2

This is one way to do it:

a = 'ThisIsACamelCaseString'
b = [i for i, e in enumerate(a) if e.isupper()] + [len(a)]
c = [a[x: y] for x, y in zip(b, b[1:])] 
final = ['(Empty)']*10
for i, case in enumerate(c):
    final[i] = case
zipa
  • 27,316
  • 6
  • 40
  • 58
0

Use regular expressions to split camel case How to do CamelCase split in python .

Or just iterate over the string in a loop.

Community
  • 1
  • 1
Serge
  • 3,387
  • 3
  • 16
  • 34