7/15 : i want to optimize my prog (i want less steps)


#1

<Below this line, add a link to the EXACT exercise that you are stuck at.>

https://www.codecademy.com/fr/courses/learn-python/lessons/practice-makes-perfect/exercises/reverse?action=lesson_resume

<In what way does your code behave incorrectly? Include ALL error messages.>

all work as well

<What do you expect to happen instead?>

my program is to long … i want it do the same in less lines / steps (without counting my debug code lines who helped me to do what i did)


def join_strings(tab): #function for concatenate c in tab2
  result = ""
  for c in tab:
    result +=  c
  return result

def reverse(text):
  #print len(text), ": text lenght" #debug
  index = len(text)-1
  #print index, ": index use" #debug
  tab=[]
  tab2=[]
  for c in text:
    tab.append(c)
    tab2.append(c) #for create a tab of the same lenght
  #print tab, ": tab1" #debug
  for i in tab:
    tab2[index] = i
    #print tab2, ": tab2 " #debug
    index -=1
    #print index, ": l'index d'ecremente" #debug
  return join_strings(tab2) #concatenates strings
 
print reverse("sebaStieN")




#2

Python already implements string concatenation and reversing though, so making it shorter would be using that (hard to find a meaningful in-between, because that’s where you’ll end up)

You’re not doing anything outrageously redundant, I’d say that’s fine if I asked you to write those functions without using the built-in versions of them

If I were to complain, it’d be about code style, you could run flake8 or something on it


#3

so i did a great jobs ? :smiley:
i just choice to did it in 2 functions instead of 1 asked :

  • one that reserve letters
  • one that concatened letters

i call just the reverse function because in her process she call already the concatenation function.

edit : “flake8” ?


#4

It’s a style checker. Its output for your code:

c.py:1:23: E261 at least two spaces before inline comment
c.py:1:24: E262 inline comment should start with '# '
c.py:2:3: E111 indentation is not a multiple of four
c.py:3:3: E111 indentation is not a multiple of four
c.py:4:14: E222 multiple spaces after operator
c.py:5:3: E111 indentation is not a multiple of four
c.py:7:1: E302 expected 2 blank lines, found 1
c.py:8:3: E114 indentation is not a multiple of four (comment)
c.py:8:3: E265 block comment should start with '# '
c.py:9:3: E111 indentation is not a multiple of four
c.py:10:3: E114 indentation is not a multiple of four (comment)
c.py:10:3: E265 block comment should start with '# '
c.py:11:3: E111 indentation is not a multiple of four
c.py:11:6: E225 missing whitespace around operator
c.py:12:3: E111 indentation is not a multiple of four
c.py:12:7: E225 missing whitespace around operator
c.py:13:3: E111 indentation is not a multiple of four
c.py:15:19: E261 at least two spaces before inline comment
c.py:15:20: E262 inline comment should start with '# '
c.py:16:3: E114 indentation is not a multiple of four (comment)
c.py:16:3: E265 block comment should start with '# '
c.py:17:3: E111 indentation is not a multiple of four
c.py:19:5: E265 block comment should start with '# '
c.py:20:13: E225 missing whitespace around operator
c.py:21:5: E265 block comment should start with '# '
c.py:22:3: E111 indentation is not a multiple of four
c.py:22:28: E261 at least two spaces before inline comment
c.py:22:29: E262 inline comment should start with '# '
c.py:23:1: W293 blank line contains whitespace
c.py:24:1: E305 expected 2 blank lines after class or function definition, found 1

#5

i just add #debug here for you.

in my final code its without :


def join_strings(tab): #function for concatenate c in tab2
  result = ""
  for c in tab:
    result +=  c
  return result

def reverse(text):
  #print len(text), ": text lenght" 
  index = len(text)-1
  #print index, ": index use" 
  tab=[]
  tab2=[]
  for c in text:
    tab.append(c)
    tab2.append(c) #for create a tab of the same lenght
  #print tab, ": tab1"
  for i in tab:
    tab2[index] = i
    #print tab2, ": tab2 " 
    index -=1
    #print index, ": l'index d'ecremente"
  return join_strings(tab2) #concatenates strings
 
print reverse("sebaStieN")



and during my work in progress : (so imagime the prog doesnt work as well and “debug print” help me to see what happend when i run the prog)


def join_strings(tab): #function for concatenate c in tab2
  result = ""
  for c in tab:
    result +=  c
  return result

def reverse(text):
  print len(text), ": text lenght" 
  index = len(text)-1
  print index, ": index use" 
  tab=[]
  tab2=[]
  for c in text:
    tab.append(c)
    tab2.append(c) #for create a tab of the same lenght
  print tab, ": tab1" 
  for i in tab:
    tab2[index] = i
    print tab2, ": tab2 " 
    index -=1
    print index, ": l'index d'ecremente"
  return join_strings(tab2) #concatenates strings
 
print reverse("sebaStieN")




#6

how i should use it ? i can’t import it and “# flake8: noqa” just put what is after “#” in comments …


#7

Your question is a bit unclear.
Not sure why you’d want to use # noqa (or even where you got that from)
The output I posted above says what “should” be changed about your code
You’d need to install it to import it. There are probably online versions of it if you google. How you’d install it depends somewhat on your os/preferences. But generally you’d install it with pip install --user pylint

You don’t need to install it to just fix up THIS code though, since I already posted the output.


#8

i only use the python implanted on your website.

as long as i don’t have install a software for coding : i install nothing ^^

the day codecademy will ask me to DL something : i will install what they ask me ^^

edit : but yeah i’m instering to know wich software allow me to code in python :wink:

and you try to said me that “flake8” is a sofware ? or i dont get it ?

i’m french … and my english understanding is limited :c


#9

Yeah flake8 is a program. You don’t need it. It can help make your code prettier, that’s all.

A text editor and a python interpreter. You’d also need a shell (terminal) or something so that you can execute Python.

People have very different opinions about what text editors suit them, so nobody can really tell you what to use. Atom or VS Code are reasonable to start off with. I’d recommend *against* Sublime Text (proprietary) and Notepad++ (windows only). Beginners using Windows often use IDLE which comes with Python (Windows users new to programming often have a hard time with basic tasks like editing text and running programs because Windows hides these kind of things behind graphical buttons while at the same time making text-interfaces hard to use/scary) … IDLE is kind of a toy editor though.


#10

ok thanks for this informations.

i will try notepad++ that i already used.

what i will need for interpreter ? Atom or VS Code are interpretor you speak about ?

what i can use as shell ? IDLE is the shell you speak about ?

Sorry is it 2 a.m here and i’m stopping get stick on pc all the night because i need a Job / activity daily rythm and you already know that my english is not as well as i would for understanding what you give me as informations :confused:


#11

Atom/VS Code are text editors, just like Notepad++.
IDLE is also a text editor.

Shells are text-only interfaces to your computer. They are languages for common tasks like executing programs and handling their input/output streams which might point at the terminal, other programs, or files. Think of a shell as a start menu, except everything is text and it has variables, functions, loops etc.

With the ability to start programs yourself (instead of relying on gui’s to do it for you), you need two more things:

  1. text editor
  2. python

And then writing and running code goes like (click it to replay):


(vim is my editor, you’d switch that out with something like notepad++)

If you’re on windows then you might use powershell or cmd.exe - if you know how to set up bash, then that’s probably preferable (because that’s what everything else uses).
On anything else you’d look for some application named terminal or similar.

python is obtained from python.org, if you’re on windows. Otherwise your os provides it.
On windows, I think the installer might have an option for adding python to your PATH variable which is very important (otherwise typing “python” won’t find anything)


#12

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.