Displaying a sentence letter by letter including spaces [on hold]











up vote
1
down vote

favorite












Can I make this simpler anyhow?



My code displays it correctly:



v = lambda x: [x[0:i+1] for i in range(len(x))]+[x[0:-i-1] for i in range(len(x))]  
print(*v(input("Enter a sentence: ")), sep="n")


This also works:





text = input("Enter a sentence: ")
v = [text[:i] for i in range(1, len(text)+1)]
print(*(v + v[::-1]), sep='n')


This is what it prints:



Enter something: test
t
te
tes
test
tes
te
t









share|improve this question









New contributor




Samardeep Khurana is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











put on hold as off-topic by Josay, janos, Sᴀᴍ Onᴇᴌᴀ, яүυк, Gerrit0 2 hours ago


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Authorship of code: Since Code Review is a community where programmers improve their skills through peer review, we require that the code be posted by an author or maintainer of the code, that the code be embedded directly, and that the poster know why the code is written the way it is." – janos, Sᴀᴍ Onᴇᴌᴀ, яүυк, Gerrit0

If this question can be reworded to fit the rules in the help center, please edit the question.













  • Csn you just suggest an easier version of the first one
    – Samardeep Khurana
    12 hours ago






  • 2




    Have you written this yourself and do you fully understand how it works?
    – janos
    11 hours ago






  • 1




    not exactly to be honest
    – Samardeep Khurana
    11 hours ago















up vote
1
down vote

favorite












Can I make this simpler anyhow?



My code displays it correctly:



v = lambda x: [x[0:i+1] for i in range(len(x))]+[x[0:-i-1] for i in range(len(x))]  
print(*v(input("Enter a sentence: ")), sep="n")


This also works:





text = input("Enter a sentence: ")
v = [text[:i] for i in range(1, len(text)+1)]
print(*(v + v[::-1]), sep='n')


This is what it prints:



Enter something: test
t
te
tes
test
tes
te
t









share|improve this question









New contributor




Samardeep Khurana is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











put on hold as off-topic by Josay, janos, Sᴀᴍ Onᴇᴌᴀ, яүυк, Gerrit0 2 hours ago


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Authorship of code: Since Code Review is a community where programmers improve their skills through peer review, we require that the code be posted by an author or maintainer of the code, that the code be embedded directly, and that the poster know why the code is written the way it is." – janos, Sᴀᴍ Onᴇᴌᴀ, яүυк, Gerrit0

If this question can be reworded to fit the rules in the help center, please edit the question.













  • Csn you just suggest an easier version of the first one
    – Samardeep Khurana
    12 hours ago






  • 2




    Have you written this yourself and do you fully understand how it works?
    – janos
    11 hours ago






  • 1




    not exactly to be honest
    – Samardeep Khurana
    11 hours ago













up vote
1
down vote

favorite









up vote
1
down vote

favorite











Can I make this simpler anyhow?



My code displays it correctly:



v = lambda x: [x[0:i+1] for i in range(len(x))]+[x[0:-i-1] for i in range(len(x))]  
print(*v(input("Enter a sentence: ")), sep="n")


This also works:





text = input("Enter a sentence: ")
v = [text[:i] for i in range(1, len(text)+1)]
print(*(v + v[::-1]), sep='n')


This is what it prints:



Enter something: test
t
te
tes
test
tes
te
t









share|improve this question









New contributor




Samardeep Khurana is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











Can I make this simpler anyhow?



My code displays it correctly:



v = lambda x: [x[0:i+1] for i in range(len(x))]+[x[0:-i-1] for i in range(len(x))]  
print(*v(input("Enter a sentence: ")), sep="n")


This also works:





text = input("Enter a sentence: ")
v = [text[:i] for i in range(1, len(text)+1)]
print(*(v + v[::-1]), sep='n')


This is what it prints:



Enter something: test
t
te
tes
test
tes
te
t






python python-3.x






share|improve this question









New contributor




Samardeep Khurana is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




Samardeep Khurana is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited 3 hours ago









Jamal

30.2k11115226




30.2k11115226






New contributor




Samardeep Khurana is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 13 hours ago









Samardeep Khurana

92




92




New contributor




Samardeep Khurana is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Samardeep Khurana is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Samardeep Khurana is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




put on hold as off-topic by Josay, janos, Sᴀᴍ Onᴇᴌᴀ, яүυк, Gerrit0 2 hours ago


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Authorship of code: Since Code Review is a community where programmers improve their skills through peer review, we require that the code be posted by an author or maintainer of the code, that the code be embedded directly, and that the poster know why the code is written the way it is." – janos, Sᴀᴍ Onᴇᴌᴀ, яүυк, Gerrit0

If this question can be reworded to fit the rules in the help center, please edit the question.




put on hold as off-topic by Josay, janos, Sᴀᴍ Onᴇᴌᴀ, яүυк, Gerrit0 2 hours ago


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Authorship of code: Since Code Review is a community where programmers improve their skills through peer review, we require that the code be posted by an author or maintainer of the code, that the code be embedded directly, and that the poster know why the code is written the way it is." – janos, Sᴀᴍ Onᴇᴌᴀ, яүυк, Gerrit0

If this question can be reworded to fit the rules in the help center, please edit the question.












  • Csn you just suggest an easier version of the first one
    – Samardeep Khurana
    12 hours ago






  • 2




    Have you written this yourself and do you fully understand how it works?
    – janos
    11 hours ago






  • 1




    not exactly to be honest
    – Samardeep Khurana
    11 hours ago


















  • Csn you just suggest an easier version of the first one
    – Samardeep Khurana
    12 hours ago






  • 2




    Have you written this yourself and do you fully understand how it works?
    – janos
    11 hours ago






  • 1




    not exactly to be honest
    – Samardeep Khurana
    11 hours ago
















Csn you just suggest an easier version of the first one
– Samardeep Khurana
12 hours ago




Csn you just suggest an easier version of the first one
– Samardeep Khurana
12 hours ago




2




2




Have you written this yourself and do you fully understand how it works?
– janos
11 hours ago




Have you written this yourself and do you fully understand how it works?
– janos
11 hours ago




1




1




not exactly to be honest
– Samardeep Khurana
11 hours ago




not exactly to be honest
– Samardeep Khurana
11 hours ago










1 Answer
1






active

oldest

votes

















up vote
1
down vote













Since the program is only a snippet, there's not much to talk about in terms of larger design choices, but there are a few things of note:





  • Inconsistency / non-identical programs: Your two programs are not identical: the first one produces the original word once, while the second version produces the ascending list and then the same list reversed; hence, the original word appears twice. This could be fixed by tweaking the first list to remove the last element, like v[:-1]. You can read more about list slices here; they're generally a useful tool.


  • Efficiency: Unless having the list for a later part of the program is important, it can be made more efficient by using iterators. Iterators can output a pattern's elements one-by-one without storing the entire pattern in memory. For a small program like this, it doesn't make any difference, but being aware of the technique is useful as the situations scale up. This SO answer explains the rationale of using iterators in a bit more detail. Using iterators, one could write:



    import itertools as it
    intext = input("Enter a sentence: ")
    for s in it.chain((intext[:i] for i in range(1, len(intext))),
    (intext[:i] for i in range(len(intext), 0, -1))):
    print(s)


    Here, itertools (an extremely useful standard library, probably one of the most commonly used standard libraries), is used to chain two two iterators together so they are iterated one after another using the nicely named chain function. The mid-line indent is used to limit the line length, and complies with PEP 8's guidelines about indenting split lines.



  • Other critiques: Concatenating two lists together is generally something to avoid if you can, because it's somewhat inefficient. Lambda functions should probably be used sparingly unless they offer a utility over functions in your particular situation. Unpacking argument lists is clever, but in this situation, it's probably more clear and efficient to just iterate through a for-loop as shown above.





If you want to learn more Python, I highly recommend starting to read through portions of the Python docs as they become relevant. They're generally very well-written and easy to understand. Reading Stack Overflow questions to find solutions to specific programming problems also helps, but the docs help fill in the gaps.






share|improve this answer






























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    1
    down vote













    Since the program is only a snippet, there's not much to talk about in terms of larger design choices, but there are a few things of note:





    • Inconsistency / non-identical programs: Your two programs are not identical: the first one produces the original word once, while the second version produces the ascending list and then the same list reversed; hence, the original word appears twice. This could be fixed by tweaking the first list to remove the last element, like v[:-1]. You can read more about list slices here; they're generally a useful tool.


    • Efficiency: Unless having the list for a later part of the program is important, it can be made more efficient by using iterators. Iterators can output a pattern's elements one-by-one without storing the entire pattern in memory. For a small program like this, it doesn't make any difference, but being aware of the technique is useful as the situations scale up. This SO answer explains the rationale of using iterators in a bit more detail. Using iterators, one could write:



      import itertools as it
      intext = input("Enter a sentence: ")
      for s in it.chain((intext[:i] for i in range(1, len(intext))),
      (intext[:i] for i in range(len(intext), 0, -1))):
      print(s)


      Here, itertools (an extremely useful standard library, probably one of the most commonly used standard libraries), is used to chain two two iterators together so they are iterated one after another using the nicely named chain function. The mid-line indent is used to limit the line length, and complies with PEP 8's guidelines about indenting split lines.



    • Other critiques: Concatenating two lists together is generally something to avoid if you can, because it's somewhat inefficient. Lambda functions should probably be used sparingly unless they offer a utility over functions in your particular situation. Unpacking argument lists is clever, but in this situation, it's probably more clear and efficient to just iterate through a for-loop as shown above.





    If you want to learn more Python, I highly recommend starting to read through portions of the Python docs as they become relevant. They're generally very well-written and easy to understand. Reading Stack Overflow questions to find solutions to specific programming problems also helps, but the docs help fill in the gaps.






    share|improve this answer



























      up vote
      1
      down vote













      Since the program is only a snippet, there's not much to talk about in terms of larger design choices, but there are a few things of note:





      • Inconsistency / non-identical programs: Your two programs are not identical: the first one produces the original word once, while the second version produces the ascending list and then the same list reversed; hence, the original word appears twice. This could be fixed by tweaking the first list to remove the last element, like v[:-1]. You can read more about list slices here; they're generally a useful tool.


      • Efficiency: Unless having the list for a later part of the program is important, it can be made more efficient by using iterators. Iterators can output a pattern's elements one-by-one without storing the entire pattern in memory. For a small program like this, it doesn't make any difference, but being aware of the technique is useful as the situations scale up. This SO answer explains the rationale of using iterators in a bit more detail. Using iterators, one could write:



        import itertools as it
        intext = input("Enter a sentence: ")
        for s in it.chain((intext[:i] for i in range(1, len(intext))),
        (intext[:i] for i in range(len(intext), 0, -1))):
        print(s)


        Here, itertools (an extremely useful standard library, probably one of the most commonly used standard libraries), is used to chain two two iterators together so they are iterated one after another using the nicely named chain function. The mid-line indent is used to limit the line length, and complies with PEP 8's guidelines about indenting split lines.



      • Other critiques: Concatenating two lists together is generally something to avoid if you can, because it's somewhat inefficient. Lambda functions should probably be used sparingly unless they offer a utility over functions in your particular situation. Unpacking argument lists is clever, but in this situation, it's probably more clear and efficient to just iterate through a for-loop as shown above.





      If you want to learn more Python, I highly recommend starting to read through portions of the Python docs as they become relevant. They're generally very well-written and easy to understand. Reading Stack Overflow questions to find solutions to specific programming problems also helps, but the docs help fill in the gaps.






      share|improve this answer

























        up vote
        1
        down vote










        up vote
        1
        down vote









        Since the program is only a snippet, there's not much to talk about in terms of larger design choices, but there are a few things of note:





        • Inconsistency / non-identical programs: Your two programs are not identical: the first one produces the original word once, while the second version produces the ascending list and then the same list reversed; hence, the original word appears twice. This could be fixed by tweaking the first list to remove the last element, like v[:-1]. You can read more about list slices here; they're generally a useful tool.


        • Efficiency: Unless having the list for a later part of the program is important, it can be made more efficient by using iterators. Iterators can output a pattern's elements one-by-one without storing the entire pattern in memory. For a small program like this, it doesn't make any difference, but being aware of the technique is useful as the situations scale up. This SO answer explains the rationale of using iterators in a bit more detail. Using iterators, one could write:



          import itertools as it
          intext = input("Enter a sentence: ")
          for s in it.chain((intext[:i] for i in range(1, len(intext))),
          (intext[:i] for i in range(len(intext), 0, -1))):
          print(s)


          Here, itertools (an extremely useful standard library, probably one of the most commonly used standard libraries), is used to chain two two iterators together so they are iterated one after another using the nicely named chain function. The mid-line indent is used to limit the line length, and complies with PEP 8's guidelines about indenting split lines.



        • Other critiques: Concatenating two lists together is generally something to avoid if you can, because it's somewhat inefficient. Lambda functions should probably be used sparingly unless they offer a utility over functions in your particular situation. Unpacking argument lists is clever, but in this situation, it's probably more clear and efficient to just iterate through a for-loop as shown above.





        If you want to learn more Python, I highly recommend starting to read through portions of the Python docs as they become relevant. They're generally very well-written and easy to understand. Reading Stack Overflow questions to find solutions to specific programming problems also helps, but the docs help fill in the gaps.






        share|improve this answer














        Since the program is only a snippet, there's not much to talk about in terms of larger design choices, but there are a few things of note:





        • Inconsistency / non-identical programs: Your two programs are not identical: the first one produces the original word once, while the second version produces the ascending list and then the same list reversed; hence, the original word appears twice. This could be fixed by tweaking the first list to remove the last element, like v[:-1]. You can read more about list slices here; they're generally a useful tool.


        • Efficiency: Unless having the list for a later part of the program is important, it can be made more efficient by using iterators. Iterators can output a pattern's elements one-by-one without storing the entire pattern in memory. For a small program like this, it doesn't make any difference, but being aware of the technique is useful as the situations scale up. This SO answer explains the rationale of using iterators in a bit more detail. Using iterators, one could write:



          import itertools as it
          intext = input("Enter a sentence: ")
          for s in it.chain((intext[:i] for i in range(1, len(intext))),
          (intext[:i] for i in range(len(intext), 0, -1))):
          print(s)


          Here, itertools (an extremely useful standard library, probably one of the most commonly used standard libraries), is used to chain two two iterators together so they are iterated one after another using the nicely named chain function. The mid-line indent is used to limit the line length, and complies with PEP 8's guidelines about indenting split lines.



        • Other critiques: Concatenating two lists together is generally something to avoid if you can, because it's somewhat inefficient. Lambda functions should probably be used sparingly unless they offer a utility over functions in your particular situation. Unpacking argument lists is clever, but in this situation, it's probably more clear and efficient to just iterate through a for-loop as shown above.





        If you want to learn more Python, I highly recommend starting to read through portions of the Python docs as they become relevant. They're generally very well-written and easy to understand. Reading Stack Overflow questions to find solutions to specific programming problems also helps, but the docs help fill in the gaps.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited 3 hours ago

























        answered 4 hours ago









        Graham

        576113




        576113















            Popular posts from this blog

            Morgemoulin

            Scott Moir

            Souastre