Hack Forums
[Python] Python task - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [Python] Python task (/showthread.php?tid=5947348)



Python task - blackcoder00 - Mar 4, 2019

Hi guys first i'm new in python and second i started to create the algorithm of this task so i'm asking how to transform my algorithm into a script python for this task ?i want to test the code to get an idea of python language like i did in pascal i need help guys
Pascal code :
PHP Code

<?php 
Function ReverseString(ch: string): string;
var
    
i,j:longint;
begin
  setlength
(result,length(ch));
  
i:=1; j:=length(cht);
  while (
i<=j) do
    
begin
      result
[i]:=cht[j-i+1];
      
inc(i);
    
end;
end; 
PHP Code

<?php 
if count <> 0 then
    average 
:= sum / count; 
    
WriteLn('The average: ' + FormatFloat('0.00', average));
  
WriteLn();Write('Press ENTER to quit.');
   
ReadLn; 
PHP Code

<?php 
min
:=T[1];
max:=T[1];
for 
i:=2 to n do
begin
if(T[i]<=min) then min:=T[i];
if(
T[i]>=max) then max:=T[i]; 
PHP Code

<?php 
str
(n,ch);
x:=ch[1];
for 
i:=2 to length(ch) do
if 
ch[i]=x then
sous_chaine
(ch,i,1); 
soo guys i want to know how can i solve this task with python and thanks
[Image: 6QkkNpI.png]
[Image: ARztYGV.png]
[Image: z5U9X96.png]
[Image: 4jMteTX.png]


RE: Python task - daymon - Mar 4, 2019

Are you asking how to build the script itself or..?


RE: Python task - blackcoder00 - Mar 4, 2019

(Mar 4, 2019 05:31 PM)Sin+ Wrote:  Are you asking how to build the script itself or..?

i solved the task with pascal so i want to find all the script with python to compare them with my scripts in pascal to uunderstand the different between


(Mar 4, 2019 05:31 PM)Sin+ Wrote:  Are you asking how to build the script itself or..?

so yeah i want to see this task coded with python for comparing with pascal code and ty


Just take a look bro what is wrong with my code
[Image: 1Er6WYy.png][Image: ZPIqsez.png][Image: yg8XG25.png]


RE: Python task - nicodxmus - Mar 4, 2019

In test2 u have just a simple mistake, you don't have any return.
Code
no_list = [22, 68, 90, 78, 90, 88]


def average(x):
   return sum(x) / len(x)

print(average(no_list))
This code is the simplest solution. You don't have to create these variables, but if you really want you just must put this at the end function:
Code
return summing/count
Also in this exercise, this if-statement isn't necessarily cuz you have a list filled already.

EDIT:
The third exercise again no return just add
Code
return max
At the end of the function and it will work
Full code:
Code
no_list = [1,2,3,4,8,3]

def maximum(no_list):
    max = 0
    for i in no_list:
        if i>max:
            max = i
    return max

print(maximum(no_list))



RE: Python task - blackcoder00 - Mar 4, 2019

(Mar 4, 2019 08:03 PM)nicodxmus Wrote:  In test2 u have just a simple mistake, you don't have any return.
Code
no_list = [22, 68, 90, 78, 90, 88]


def average(x):
   return sum(x) / len(x)

print(average(no_list))
This code is the simplest solution. You don't have to create these variables, but if you really want you just must put this at the end function:
Code
return summing/count
Also in this exercise, this if-statement isn't necessarily cuz you have list filled already.

ty bro what about the other 3,4 script task btw i send u 20Bytes for helping


RE: Python task - nicodxmus - Mar 4, 2019

And in the fourth exercise, I can't understand what's your idea in this algorithm but I can show you how to make it easier:
Code
no_list = [1,2,10,3,4,8,3,9,2,3,2,1,4]

def unique_list(l):
   return list(set(l))

print(unique_list(no_list))
It will also sort number from lowest to highiest.
You have good ideas but when you are doing stuff like this where you are printing values from functions you always have to return something.


RE: Python task - blackcoder00 - Mar 4, 2019

(Mar 4, 2019 08:30 PM)nicodxmus Wrote:  And in the fourth exercise, I can't understand what's your idea in this algorithm but I can show you how to make it easier:
Code
no_list = [1,2,10,3,4,8,3,9,2,3,2,1,4]

def unique_list(l):
   return list(set(l))

print(unique_list(no_list))
It will also sort number from lowest to highiest.
You have good ideas but when you are doing stuff like this where you are printing values from functions you always have to return something.

ty bro for eveything i have done it by myself it's my first day with python XD
PHP Code

<?php 
no_list 
= [22,22,2,1,11,11,2,2,3,3,3,4,5,5,5,55,55,66]
s = []

def unique_list(l):
    for 
i in l:
       if 
i not in s:
          
s.append(i)
          
    return 
s;
print(
unique_list(no_list)) 



RE: Python task - datguy.dev - Mar 6, 2019

(Mar 4, 2019 09:03 PM)blackcoder00 Wrote:  ty bro for eveything i have done it by myself it's my first day with python XD
PHP Code

<?php 
no_list 
= [22,22,2,1,11,11,2,2,3,3,3,4,5,5,5,55,55,66]
s = []

def unique_list(l):
 
   for i in l:
 
      if i not in s:
 
         s.append(i)
 
         
    return s
;
print(
unique_list(no_list)) 

You're using the variable s globally, when it should probably be inside the scope of function unique_list() above the for loop. Otherwise, it should be s = unique_list(no_list) then print(s).