code wars - JWalshe86/career GitHub Wiki

code wars

Python Tutor

Unique in Order

Implement the function unique_in_order which takes as argument a sequence and returns a list of items without any elements with the same value next to each other and preserving the original order of elements.

For example:

unique_in_order('AAAABBBCCDAABBB') == ['A', 'B', 'C', 'D', 'A', 'B'] unique_in_order('ABBCcAD') == ['A', 'B', 'C', 'c', 'A', 'D'] unique_in_order([1, 2, 2, 3, 3]) == [1, 2, 3] unique_in_order((1, 2, 2, 3, 3)) == [1, 2, 3]

solution

def unique_in_order(*sequence):
    for x in sequence:
        
        return x

x = unique_in_order(12,12,4,3)

print('x', x)

image Using * got rid of the multiple args being passed into one parameter error

🐛 AttributeError: 'str' object has no attribute 'push'

def unique_in_order(*sequence):   
    list=[] 
    for x in sequence:
        list.append(x)
        if list[0][0] == list[0][1]:
            print('hi')
    
    return list

x = unique_in_order('AAAABBBCCDAABBB')

print('list', x)

image

Trying this Add values into an empty list from python for loop

def unique_in_order(*sequence):
    unique_list = []
    for x in sequence:
        unique_list.append(x)


    return unique_list

unique_list = unique_in_order(12,12,4,3)

print('unique_list', unique_list)

image

Looked at appending-an-id-to-a-list-if-not-already-present-in-the-list image

def unique_in_order(*sequence):
    unique_list = []
    for x in sequence:
        unique_list.append(x) if x not in unique_list else unique_list

    return unique_list

unique_list = unique_in_order(12,12,4,3)

print('unique_list', unique_list)

gives image

However inserting 'AAAABBBCCDAABBB' gives: image This is because all the values here as seen as one.

how-do-i-split-a-string-into-a-list-of-characters image

def unique_in_order(*sequence):
    unique_list = []
    for x in sequence:
        unique_list.append(x) if x not in unique_list else unique_list

    unique_list = unique_list[0]
    unique_list = list(unique_list)
    
    return list(unique_list)

unique_list = unique_in_order('AAAABBBCCDAABBB')

print('unique_list', unique_list)

image

def unique_in_order(*sequence):
    unique_list = []
    sequence = sequence[0]
    for x in sequence:
        unique_list.append(x) if x not in unique_list else unique_list

    
    return list(unique_list)

unique_list = unique_in_order('AAAABBBCCDAABBB')

print('unique_list', unique_list)

image

def unique_in_order(*sequence):
    unique_list = []
    sequence = sequence[0]
    for x in sequence:
        unique_list.append(x) if x not in unique_list else unique_list

    
    return list(unique_list)

However, I don't want to print [ or , as unique

unique_list = unique_in_order('[1, 2, 2, 3, 3]')

print('unique_list', unique_list)

image

image

def unique_in_order(*sequence):
    unique_list = []
    sequence = sequence[0]
    for x in sequence:
        unique_list.append(x) if x.isalpha() or x.isdigit() and x not in unique_list else unique_list

    
    return list(unique_list)

unique_list = unique_in_order('[1, 2, 2, 3, 3]')

print('unique_list', unique_list)

image

However this no longer works image

🎉 All 4 entries pass the test with the following code:

def unique_in_order(*sequence):
    unique_list = []
    sequence = sequence[0]
    for x in sequence:
        if x.isalpha() or x.isdigit():
            unique_list.append(x) if x not in unique_list else unique_list

    
    return list(unique_list)

unique_list = unique_in_order('(1, 2, 2, 3, 3)')

print('unique_list', unique_list)

Better Than Average

There was a test in your class and you passed it. Congratulations!

But you're an ambitious person. You want to know if you're better than the average student in your class.

You receive an array with your peers' test scores. Now calculate the average and compare your score!

Return true if you're better, else false!

Note: Your points are not included in the array of your class's points. Do not forget them when calculating the average score!

Tests

test.describe("Basic Tests")

test.it("better_than_average([2, 3], 5) should return True")
test.assert_equals(better_than_average([2, 3], 5), True)

test.it("better_than_average([100, 40, 34, 57, 29, 72, 57, 88], 75) should return True")
test.assert_equals(better_than_average([100, 40, 34, 57, 29, 72, 57, 88], 75), True)

test.it("better_than_average([12, 23, 34, 45, 56, 67, 78, 89, 90], 69) should return True")
test.assert_equals(better_than_average([12, 23, 34, 45, 56, 67, 78, 89, 90], 69), True)

test.it("better_than_average([41, 75, 72, 56, 80, 82, 81, 33], 50) should return False")
test.assert_equals(better_than_average([41, 75, 72, 56, 80, 82, 81, 33], 50), False)

test.it("better_than_average([29, 55, 74, 60, 11, 90, 67, 28], 21) should return False")
test.assert_equals(better_than_average([29, 55, 74, 60, 11, 90, 67, 28], 21), False)

def better_than_average(class_points, your_points):
    total_class = []
    for point in class_points:
        total_class.append(point)
        av = (sum(total_class) + your_points)/len(total_class)+1

        if(your_points > av):
            return True
        else:
            False

x = better_than_average([41, 75, 72, 56, 80, 82, 81, 33], 50)
print(x)

This worked for sum of 41, 75, 72, 56, 80, 82, 81, 33 = 520 Add 50 & divide by 9 = 63.3%

def better_than_average(class_points, your_points):
    total_class = []
    for point in class_points:
        total_class.append(point)
    total_class.append(50)
    q = len(total_class)
    av = sum(total_class) / q
    return av


x = better_than_average([41, 75, 72, 56, 80, 82, 81, 33], 50)
print(x)


# sum of 41, 75, 72, 56, 80, 82, 81, 33 = 520 Add 50 & divide by 9 = 63.3%

Count by X

Create a function with two arguments that will return an array of the first n multiples of x.

Assume both the given number and the number of times to count will be positive numbers greater than 0.

Return the results as an array or list ( depending on language ).

        test.assert_equals(count_by(1, 5), [1, 2, 3, 4, 5]) 🆗 
        test.assert_equals(count_by(2, 5), [2, 4, 6, 8, 10])
        test.assert_equals(count_by(3, 5), [3, 6, 9, 12, 15])
        test.assert_equals(count_by(50, 5), [50, 100, 150, 200, 250])
        test.assert_equals(count_by(100, 5), [100, 200, 300, 400, 500])

def count_by(x, n):
// insert code here

This works for 1,5 The first argument in range starts it at index 1, then adding 1 to the n means it stops & makes up for the zero..

image

Not working for 2,5 test.assert_equals(count_by(2, 5), [2, 4, 6, 8, 10])

def count_by(x, n):
    list = []
    for i in range(x,n+1):
//range here (2,6) equates to 2,3,4,5, the third step parameter could be (x*x-1)?
        list.append(x)
        
    return list

x = count_by(2, 5)
print('list', x)

Gives: image

Using n+x means the correct number of numbers in the list is returned each time

def count_by(x, n):
    list = []
    for i in range(x,n+x):
        list.append(x)
        
    return list

x = count_by(3, 5)
print('list', x)

This works for 1,5

def count_by(x, n):
    list = []
    for i in range(x,n+x):
        z = x*i
        print(i)
        list.append(z)
        
    return list

x = count_by(1, 5)
print('list', x)

This works for (1,5),(2,5),(3,5)** actually works for all tests! -> the extra y iterator gives a continuous range that starts from 1

def count_by(x, n):
    list = []
    y = 0
    for i in range(x,n+x):
        y = y + 1
        if x < 2:
            z = x*i
        else:
            z = x*y
        list.append(z)
        
    return list

x = count_by(1, 5)
print('list', x)

Print error

In a factory a printer prints labels for boxes. For one kind of boxes the printer has to use colors which, for the sake of simplicity, are named with letters from a to m.

The colors used by the printer are recorded in a control string. For example a "good" control string would be aaabbbbhaijjjm meaning that the printer used three times color a, four times color b, one time color h then one time color a...

Sometimes there are problems: lack of colors, technical malfunction and a "bad" control string is produced e.g. aaaxbbbbyyhwawiwjjjwwm with letters not from a to m.

You have to write a function printer_error which given a string will return the error rate of the printer as a string representing a rational whose numerator is the number of errors and the denominator the length of the control string. Don't reduce this fraction to a simpler expression.

The string has a length greater or equal to one and contains only letters from ato z.

Examples:
s="aaabbbbhaijjjm"
printer_error(s) => "0/14"

s="aaaxbbbbyyhwawiwjjjwwm"
printer_error(s) => "8/22"

def printer_error(s):
    y = []
    z = []
    x = list(map(chr, range(97, 110)))
    for i in x:
        y.append(i)
    else:
        z.append(i)
    k = len(z) -1
    return f'{k}/{len(x)+1}'

s="aaaxbbbbyyhwawiwjjjwwm"
x = printer_error(s)
print(x)
ts
1
import codewars_test as test
2
from solution import printer_error
3
​
4
@test.describe("printer_error")
5
def basic_tests():
6
    @test.it('Example Test Cases')
7
    def example_test_cases():
8
        s="aaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbmmmmmmmmmmmmmmmmmmmxyz"
9
        test.assert_equals(printer_error(s), "3/56")
10
        s = "kkkwwwaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbmmmmmmmmmmmmmmmmmmmxyz"
11
        test.assert_equals(printer_error(s), "6/60")
12
        s = "kkkwwwaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbmmmmmmmmmmmmmmmmmmmxyzuuuuu"
13
        test.assert_equals(printer_error(s) , "11/65")

This works for adding letters from a-m to one list, and all other letters to another list

def printer_error(s):
    y = []
    z = []
    x = list(map(chr, range(97, 110)))
    for i in s:
        if i in x:
            
            y.append(i)
        else:
            z.append(i)
    print('z',z)
    return 

s="aaaxbbbbyyhwawiwjjjwwm"
x = printer_error(s)
print(x)

This code works for all tests:

def printer_error(s):
    y = []
    z = []
    x = list(map(chr, range(97, 110)))
    for i in s:
        if i in x:
            
            y.append(i)
        else:
            z.append(i)
    k = len(s)
    return f'{len(z)}/{k}'

    return 

s="aaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbmmmmmmmmmmmmmmmmmmmxyz"
x = printer_error(s)
print(x)