Challenges

Reddit challenges in python language.

Files Code and Result

challenge1_easy

challenge2_easy

challenge3_easy

challenge4_easy

challenge5_easy

challenge6_easy

challenge7_easy

challenge8_easy

challenge9_easy

challenge10_easy

challenge11.easy

challenge12_easy

challenge13_easy

challenge14_easy

challenge15_easy

challenge16_easy

challenge17_easy

challenge18_easy

challenge19_easy

challenge20_easy

challenge21_easy

challenge22_easy

challenge23_easy

challenge25_easy

challenge26_easy

challenge27_easy

challenge28_easy

challenge29_easy

challenge30_easy

challenge31_easy

challenge32_easy

challenge33_easy

challenge34_easy

challenge35_easy

challenge36_easy

challenge37_easy

challenge38_easy

challenge39_easy

challenge40_easy

challenge41_easy

challenge42_easy

challenge44_easy

challenge45_easy

challenge46_easy

challenge47_easy

challenge48_easy

challenge49_easy

challenge50_easy

challenge51_easy

challenge52_easy

challenge53_easy

challenge54_easy

challenge55_easy

challenge56_easy

challenge57_easy

challenge58_easy

challenge59_easy

challenge60_easy

challenge61_easy

challenge62_easy

challenge63_easy

challenge64_easy

challenge65_easy

challenge66_Dev

challenge66_easy

challenge67_easy

challenge68_easy

challenge69_easy

challenge69_easyBonus

challenge70_easy

challenge71_easy.py

challenge72_easy

challenge73_easy

challenge74_easy

challenge76_easy

challenge77_easy

challenge79_easy

challenge80_easyone

challenge82_easy

challenge83_easy

challenge86_easy

challenge94_easy

challenge104_easy

challenge130_easy

challenge143_easy

challenge146_easy

challenge148_easy

challenge149_easy

challenge153_easy

challenge154_easy

challenge158_easy

challenge158_easybonus

challenge159_easy

challenge160_easy

challenge169_easy

challenge169_easybonus

challenge172_easy

challenge174_easy

challenge175_easy

challenge177_easy

challenge180_easy

challenge181_easy

challenge185_easy

challenge192_easy

challenge193_easy

challenge197_easy

challenge198_easy

challenge199_easy

challenge201_easy

challenge202_easy

challenge203_easy

challenge204_easy

challenge204_easyBonus

challenge205_easy

challenge205_easyBonu

challenge206_easy

challenge208_easy

challenge210_easy

challenge211_easy

challenge212_easy

challenge212_easybonus

challenge213_easy

challenge214_easy

challenge215_easy

challenge216_easy

challenge217_easy

challenge218_easy

challenge218_easyBonus1

challenge218_easyBunus2

challenge219_easy

challenge220_easy.py

challenge221_easy

challenge221_easy

challenge222_easy

challenge222_easy.py

challenge223_easy

challenge226_easy

challenge226_easy

challenge228_easy.py

challenge228_easydev

challenge229_easy

challenge229_easybonus.py

challenge232_easy

challenge232_easy

challenge232_easybonus

challenge232_easybonus

challenge234_easy

challenge234_easybonus

challenge235_easy

challenge236_easy

challenge237_easy

challenge238_easy

challenge238_easybonus

challenge239_easy

challenge240_easy

challenge242_easy

challenge242_easy

challenge242_easyBonus

challenge243_easy

challenge245_easy

challenge245_easyBonus

challenge246_easy

challenge247_easy

challenge249_easy

challenge252_easy

challenge254_easy

challenge255_easy

challenge270_easy

challenge284_easy

challenge286_easy

challenge287_easy

challenge288_easy

challenge290_easy

challenge290_easybonus

challenge291_easy

challenge291_easybonus

challenge246_easy

""" We are going to calculate how long we can light our X-mass lights with 1 battery.
First off all some quick rules in the electronics.

All things connected in parallel share the same voltage, but they have their own current.
All things connected in serial share the same current, but they have their own voltage.

Parallel:

----O----
 |     |
 ---O---

Serial:

---O---O---

We are going to use 9V batteries for our calculation. They supply a voltage of 9V (Volt)
(big surprise there) and have a capacity from around 1200mAh (milliAmpere hour).

The lifetime of the battery can be calculate by dividing the capacity by the total
Amperes we draw. E.g. If we have a 9V battery and we use a light that uses 600 mA,
we can light the light for 2 hours (1200/600)

For our lights we'll use average leds, which need an voltage of 1.7V and a current
of 20mA to operate. Since we have a 9V we can have a max of 5 leds connected in serial.
But by placing circuits in parallel, we can have more than 5 leds in total, but then
we'll drain the battery faster.

I'll split the challenges up in a few parts from here on.

Part 1

As input you'll be given the length in hours that the lights needs te be lit.
You have give me the max number of led's we can have for that time

Input

1

Output

300

Explanation:

We can have 5 leds in serial, but then they'll take only a current of 20mA.
The battery can give us 1200mA for 1 hour. So if we divide 1200 by 20 we get
that we could have 60 times 5 leds.

Inputs

1
4
8
12

Outputs

300
75
35 (37 is also possible, but then we can't have 5 leds in serial for each parallel circuit)
25

Part 2

Draw out the circuit. A led is drawn in this way -|>|-

input

20

Output

*--|>|---|>|---|>|---|>|---|>|--*
 |                             |
 --|>|---|>|---|>|---|>|---|>|--
 |                             |
 --|>|---|>|---|>|---|>|---|>|--

inputs

12
6
100

Part 3

Our circuit is not complete without a resistor to regulate the current and catch the
voltage difference. We need to calculate what the resistance should be from the resistor.
This can be done by using Ohm's law.

We know we can have 5 leds of 1.7V in series, so that is 0.5V over the resistor.
If we know the current we need we can calculate the resistance.

E.g. If we need 1 hour we can have a current of 1200 mA and we have 0.5V so the
resistance is the voltage divided by the current. => 0.5(V)/1.2(A) = 0.417 ohms

inputs

1
4
8

Outputs

0.417
1.667
3.333

Part 4

Putting it all Together

You'll be given 5 numbers, the voltage drop over a Led, the current it needs, the
voltage of the battery and the capacity and the time the leds need to be lit.

The units are in voltage V, current mA (divide by 1000 for A), voltage V, capacity (mAh), timespan h

input

1.7 20 9 1200 20

Output

Resistor: 8.333 Ohms
Scheme:
*--|>|---|>|---|>|---|>|---|>|--*
 |                             |
 --|>|---|>|---|>|---|>|---|>|--
 |                             |
 --|>|---|>|---|>|---|>|---|>|--
"""


class XmasLights:

    VOLTS = 9
    MILLIAMPS = 1200
    LED_VOLTAGE = 1.7
    LED_DRAW = 20
    LED = '-|>|-'
    LINK = '-'
    PARALLEL_LINK = '|'




    def __init__(self, hours):
        self.hours = hours
        self.total_lines = self.MILLIAMPS / (self.LED_DRAW * self.hours)
        self.led_per_line = int(self.VOLTS / self.LED_VOLTAGE)

    def num_of_leds(self):
        number_of_leds = (int(self.VOLTS / self.LED_VOLTAGE) * int(self.MILLIAMPS / self.LED_DRAW)) / self.hours
        return number_of_leds

    def draw_circuit(self):
        self.total_lines = self.MILLIAMPS / (self.LED_DRAW * self.hours)
        self.total_lines = int(self.total_lines)
        led = '-|>|-'
        link = '-'
        para = '|'
        star = '*'
        print('Scheme:')
        line = star + link + (led + link) * self.led_per_line + star
        yield line
        line_next = ' ' + link + (led + link) * self.led_per_line
        for x in range(0, self.total_lines - 1):
            spacer = ' ' + para.ljust(len(line_next) - 2, ' ') + para
            yield spacer
            yield line_next

    def resistor(self):
        volts_over_resistor = self.VOLTS - (self.LED_VOLTAGE * self.led_per_line)
        test = ((self.LED_DRAW / 1000))
        test2 = self.total_lines
        amps = ((self.LED_DRAW / 1000) * self.total_lines)
        ohm = round((volts_over_resistor / amps), 3)
        return ohm


if __name__ == "__main__":
    ans = XmasLights(20)
    ans1 = ans.resistor()
    print('{0}{1}{2})'.format('Resistor: ', ans1, ' ohms'))

    for line in ans.draw_circuit():
        print(line)

Result

Resistor: 8.333 ohms)
Scheme:
*--|>|---|>|---|>|---|>|---|>|--*
 |                             |
 --|>|---|>|---|>|---|>|---|>|--
 |                             |
 --|>|---|>|---|>|---|>|---|>|--