« Prev 10 11 12 13 14 15 16 17 18 19 20 Next »

Athlete Sort

Create Date: March 31, 2019 at 10:17 AM         Tag: PYTHON         Author Name: Sun, Charles

You are given a spreadsheet that contains a list of  athletes and their details (such as age, height, weight and so on). You are required to sort the data based on the th attribute and print the final resulting table. Follow the example given below for better understanding.

image

Note that  is indexed from  to , where  is the number of attributes.

Note: If two attributes are the same for different rows, for example, if two atheletes are of the same age, print the row that appeared first in the input.

Input Format

The first line contains  and  separated by a space. 
The next  lines each contain  elements. 
The last line contains .

Constraints

 
 
Each element 

Output Format

Print the  lines of the sorted table. Each line should contain the space separated elements. Check the sample below for clarity.

Sample Input 0

5 3
10 2 5
7 1 0
9 9 9
1 23 12
6 5 9
1

Sample Output 0

7 1 0
10 2 5
6 5 9
9 9 9
1 23 12

Explanation 0

The details are sorted based on the second attribute, since  is zero-indexed.

#!/bin/python3

import math
import os
import random
import re
import sys



if __name__ == '__main__':
    nm = input().split()

    n = int(nm[0])

    m = int(nm[1])

    arr = []

    for _ in range(n):
        arr.append(list(map(int, input().rstrip().split())))

    k = int(input())
    for row in sorted(arr, key = lambda x : x[k]):
        print(*row)

 

New Comment

Piling Up!

Create Date: March 31, 2019 at 09:08 AM         Tag: PYTHON         Author Name: Sun, Charles

There is a horizontal row of  cubes. The length of each cube is given. You need to create a new vertical pile of cubes. The new pile should follow these directions: if  is on top of  then .

When stacking the cubes, you can only pick up either the leftmost or the rightmost cube each time. Print "Yes" if it is possible to stack the cubes. Otherwise, print "No". Do not print the quotation marks.

Input Format

The first line contains a single integer , the number of test cases. 
For each test case, there are  lines. 
The first line of each test case contains , the number of cubes. 
The second line contains  space separated integers, denoting the sideLengths of each cube in that order.

Constraints 
 
 

Output Format

For each test case, output a single line containing either "Yes" or "No" without the quotes.

Sample Input

2
6
4 3 2 1 3 4
3
1 3 2

Sample Output

Yes
No

Explanation

In the first test case, pick in this order: left - , right - , left - , right - , left - , right - . 
In the second test case, no order gives an appropriate arrangement of vertical cubes.  will always come after either  or .

mehtod 1:

for _ in range(int(input())):
    n, l = int(input()), list(map(int, input().split()))
    i, j = 0, len(l) - 1
    ans = True
    pre = float("inf")
    while i < j:
        if l[i] > l[j]:
            if pre < l[i]:
                ans = False
                break
            else:
                pre = l[i]
                i += 1                
        else:
            if l[j] > pre:
                ans = False
                break
            else:
                pre = l[j]
                j -= 1                
    print("Yes" if ans else "No")

method 2:

for t in range(int(input())):
    input()
    lst = list(map(int, input().split()))
    l = len(lst)
    i = 0
    while i < l - 1 and lst[i] >= lst[i+1]:
        i += 1
    while i < l - 1 and lst[i] <= lst[i+1]:
        i += 1
    print("Yes" if i == l - 1 else "No")

method 3:

for t in range(int(input())):
    input()
    lst = [int(i) for i in input().split()]
    min_list = lst.index(min(lst))
    left = lst[:min_list]
    right = lst[min_list+1:]
    if left == sorted(left,reverse=True) and right == sorted(right):
        print("Yes")
    else:
        print("No")

method 4:

from collections import deque
for _ in (range(int(input()))):
    input()
    side_lengths = deque(map(int, input().strip().split()))
    result = "Yes"
    if max(side_lengths) not in (side_lengths[0], side_lengths[-1]):
        result = "No"
    print(result)

Deque in Python

method 5: not working

from collections import deque
for _ in (range(int(input()))):
    input()
    d = deque(map(int, input().strip().split()))
    result = "Yes"
    pre = float("inf")
    temp = 0
    while len(d) > 0:
        if d[0] >= d[-1]:
            temp = d.popleft()
            if temp > pre:
                ressult = "No"
                break
            else:
                pre = temp
        else:
            temp = d.pop()
            if temp > pre:
                result = "No"
                break
            else:
                pre = temp    
    print(result)

 

New Comment

Company Logo

Create Date: March 28, 2019 at 06:02 PM         Tag: PYTHON         Author Name: Sun, Charles

A newly opened multinational brand has decided to base their company logo on the three most common characters in the company name. They are now trying out various combinations of company names and logos based on this condition. Given a string , which is the company name in lowercase letters, your task is to find the top three most common characters in the string.

  • Print the three most common characters along with their occurrence count.
  • Sort in descending order of occurrence count.
  • If the occurrence count is the same, sort the characters in alphabetical order.

For example, according to the conditions described above,

 would have it's logo with the letters .

Input Format

A single line of input containing the string .

Constraints

Output Format

Print the three most common characters along with their occurrence count each on a separate line. 
Sort output in descending order of occurrence count. 
If the occurrence count is the same, sort the characters in alphabetical order.

Sample Input 0

aabbbccde

Sample Output 0

b 3
a 2
c 2

Explanation 0

Here, b occurs  times. It is printed first.
Both a and c occur  times. So, a is printed in the second line and c in the third line because a comes before c in the alphabet.

Note: The string  has at least  distinct characters.

method 1:

#!/bin/python3

import math
import os
import random
import re
import sys
from collections import Counter, OrderedDict


if __name__ == '__main__':
    s = input()
    class OrderedCounter(Counter, OrderedDict):
        pass
    [print(*c) for c in OrderedCounter(sorted(s)).most_common(3)]

method 2:

#!/bin/python3

import math
import os
import random
import re
import sys

from itertools import groupby

if __name__ == '__main__':
    s = input()
    print(*[char+" "+str(num) for num,char in sorted(sorted([(len(list(v)),k) for k, v in groupby(sorted(s))],key=lambda x: x[1]),key=lambda x: x[0],reverse=True)][:3], sep='\n')

method 3:

#!/bin/python3

import math
import os
import random
import re
import sys

from operator import itemgetter

if __name__ == '__main__':
    s = input()
    chars = list(s)
    d = [[c,chars.count(c)] for c in set(chars)]
    d.sort(key=itemgetter(0))
    d.sort(key=itemgetter(1), reverse=True)
    for i in d[:3]:
        print("{0} {1}".format(i[0], i[1]))

method 4:

#!/bin/python3

import math
import os
import random
import re
import sys

if __name__ == '__main__':
    s = input()
    # d:dictionary with alphabets as keys and occurance as values.
    d = {}
    for i in range(len(s)):
        if s[i] in d.keys():
            d[s[i]] +=1
        else:
            d[s[i]]=1
    # Since the output should have decreasing occurance and increasing alphabet, I use the negative occurance when sorting.
    count = sorted([tuple([-d[key], key]) for key in d.keys()])
    print(count[0][1], -count[0][0])
    print(count[1][1], -count[1][0])
    print(count[2][1], -count[2][0])

 

New Comment
« Prev 10 11 12 13 14 15 16 17 18 19 20 Next »