Rosetta Code:Solve a Task: Difference between revisions

From Rosetta Code
Content added Content deleted
(Restored deleted page text and removed commented out code.)
(Python 3 solution to 100 Doors)
Line 1: Line 1:
'''100 Doors'''
So you'd like to solve a task? Great! Here's a brief walkthrough on how you might do that. While you may already have a task and a language in mind, we're going to assume the language is "Ayrch", and the task is [[Hello world]]. If the language you're familiar with doesn't already have a [[:Category:Programming Languages|presence on Rosetta Code]], consider going through the motions of [[Rosetta Code:Add a Language|adding a language]]. If you don't have a task in mind, check out our [[:Category:Unimplemented_tasks_by_language|lists of unsolved tasks]].
''Python 3.5.2''


'''main.py:'''
=The Basics=
#100 Doors


from funcs import *
Quickly getting started, this is all you really need to do.


#'o' represents open
==Copyright==
#'c' represents closed


#Set up dictionary of doors (all open to start)
Please familiarize yourself with [[Rosetta Code:Copyrights|Rosetta Code's copyright policies]]. If you want the layman's summary: Don't post code you don't have permission to, and be aware that you're giving us (and others) permission to use it under specific conditions.
door_dict = {}
for m in range(1,101): #all doors closed
door_dict[m] = 'c'


for q in range(1,101):
==Solve the task==
frequency = q
It is best to read the task thoroughly, solve it, and check your solution ''before'' starting to edit the Rosetta Code page, (especially for tasks needing more than a very short solution).
tlist = to_toggle(frequency)
new_dict = toggle_doors(door_dict, tlist)


print(new_dict)
==Adding Code==
Language examples on each page are in alphabetical order, so you need to find where your example would fit. Once you've found that, click the "edit" link closest above the area where you want to insert your code on the task page, and add something like this to the bottom of the edit field:


----
<pre>
=={{header|Ayrch}}==
<lang Ayrch>PRINT "Goodbye, World!"</lang>
</pre>
Remember, for the sake of simplicity, we're assuming your language is Ayrch, and the task is [[Hello world]]. We're also assuming, for the moment, that Ayrch looks a lot like BASIC.
Once you've added your code, hit the preview button to make sure you crossed all your T's and closed all your tags. If the language name shows up in red (a broken link), then either the language doesn't exist on the site yet (as a category), or you misspelled/mis-capitalized the name. Check your spelling against the one in [[:Category:Programming Languages]].
That's all you really need to do!


'''funcs.py:'''
=Going a little further=


def to_toggle(x):
If you want to give your code that spit and polish shine, there are a few more steps you can take.
toggle_list = []
freq = x
to_add = freq
while to_add <= 100:
toggle_list.append(to_add)
to_add += freq
return toggle_list


def toggle_doors(doors, tlist):
==Comments and Description==
toglist = tlist
door_dict = doors
for k in toglist:
print(k)
door_num = k
if door_dict[door_num] == 'c':
door_dict[door_num] = 'o'


elif door_dict[door_num] == 'o':
Consider adding descriptions to your code examples, to help the reader understand what's going on. This is particularly helpful if your code or language paradigms are very unlike ones that are already commonly known. Regardless, it's considered good practice in any environment where you would like other people to understand what you've written.
door_dict[door_num] = 'c'

==Libraries==

return door_dict
It's perfectly all right to depend on external (or even non-standard) libraries in your code examples. However, it can be problematic for others if they don't know they need to use a library, or don't know where to find it. There's a template for that: '''libheader'''.

<pre>
=={{header|Ayrch}}==

{{libheader|Ayrch Console Extensions}}

<lang ayrch>PRINT "Goodbye World!"</lang>
</pre>

==Works With==
Not all code works with all versions of a language, all versions of a compiler, interpreter or other implementation, or even all operating systems that the language may run on. If you're aware of certain constraints or other prerequisites that haven't already been mentioned, try using the '''works with''' template.

<pre>
=={{header|Ayrch}}==

{{works with|Ayrch Virtual Machine|6.2}}

<lang ayrch>PRINT "Goodbye World!"</lang>

</pre>

=Conclusion=

Thank you for adding code, and even more thanks if you added the spit and polish to make your code shine!

==Where to go?==

Now that you've solved one task, you might like to be reminded that there are [[:Category:Unimplemented_tasks_by_language|lists of all the unsolved tasks]] for all of the languages that have a presence on Rosetta Code. If your preferred language isn't there, then you may need to go through the motions of [[Rosetta Code:Add a Language|adding a language]] in order to get the site software to automatically generate the list.

Revision as of 01:48, 20 June 2018

100 Doors Python 3.5.2

main.py:

  1. 100 Doors

from funcs import *

  1. 'o' represents open
  2. 'c' represents closed
  1. Set up dictionary of doors (all open to start)

door_dict = {} for m in range(1,101): #all doors closed

   door_dict[m] = 'c' 

for q in range(1,101):

   frequency = q
   tlist = to_toggle(frequency)
   new_dict = toggle_doors(door_dict, tlist)

print(new_dict)


funcs.py:

def to_toggle(x):

   toggle_list = []
   freq = x
   to_add = freq
   while to_add <= 100:
       toggle_list.append(to_add)
       to_add += freq  
   
   return toggle_list

def toggle_doors(doors, tlist):

       toglist = tlist        
       door_dict = doors
    
       for k in toglist:
           print(k)
           door_num = k
           
           if door_dict[door_num] == 'c':
             door_dict[door_num] = 'o'
           elif door_dict[door_num] == 'o':
             door_dict[door_num] = 'c'
       
       
       return door_dict