Skip to main content

ACS Workshop (python)

.

.

.

มาทำโจทย์กันเถอะะะะะ (โดยพรี่ภูมิ)

.nu

.

.

Question 1

  • เขียนโปรแกรมหาเส้นรอบรูปสี่เหลี่ยมผืนผ้า โดยกำหนดให้ ความกว้าง-ความยาว ของรูปสี่เหลี่ยมมา
รูปเเบบ Input
บรรทัดที่ 1 รับตัวเลขจำนวนเต็ม ความกว้าง
บรรทัดที่ 2 รับตัวเลขจำนวนเต็ม ความยาว
รูปเเบบ Output
เส้นรอบรูปสี่เหลี่ยมผืนผ้า ตอบเป็นทศนิยม 2 ตําแหน่ง
  • ตัวอย่าง
Input Output

5

10

30

25

50

150

100

200

600
def find_perimeter(width,length):
    perimeter = # your code here 
    return # your code here 

width= # Question2 input (line1)
length= # Question2 input (line2)

print(find_perimeter(width, length))


  • hint สูตรคือ 2*(w+h)
  • ใช้ round(<var>, 2) เพื่อปัดเศษเป็น 2 ตำแหน่ง

Question 2

  • หาตัวเลขในรายการ (list) ที่น้อยที่สุด 
รูปเเบบ Input
list
รูปเเบบ Output
เลขที่น้อยที่สุด
ข้อจำกัด
ห้ามใช้ built in functions เช่น min() ,ตัวเลขเป็น integer
  • ตัวอย่าง
Input Output
[17, 5, 9, 12, 2] 2
[-1, 2, 5, 15, -2] -2
[5, 0, 5, 6, 6, 4] 0
def find_minimum(numbers):
    min_num = numbers[0]
    for #your code here
        if #your code here
            #your code here
    return min_num

my_list = [17, 5, 9, 12, 2]
minimum_number = find_minimum(my_list)
print(minimum_number) 

Question 3

  • เขียนโปรแกรมหาผักจากรายการที่ให้ไป
รูปเเบบ Input
list ที่เต็มไปด้วยผักและผลไม้
รูปเเบบ Output
จำนวนของผักที่มีอยู่ใน list
  • ตัวอย่าง
Input Output
['Apple','Banana','Broccoli','Cucumber', 'Mango', 'Strawberry'] 2
[] 0
['Apple','Banana','Orange']

0

fruits = ['Apple','Banana','Orange','Mango','Strawberry']
vegetable = ['Carrot','Broccoli','Tomato','Spinach','Cucumber']

def find_thing(list):
    result = 0
    for i in list
        if #your code here
    return result

list = ['Apple','Banana','Broccoli','Cucumber','Mango','Strawberry']
print(find_thing(list))

Question 4

  • เขียนโปรแกรมคำนวนราคารวมของผักผลไม่ที่อยู่ใน list
รูปเเบบ Input
list 
รูปเเบบ Output

ราคารวมของรายการที่จะซื้อ

  • ตัวอย่าง
Input Output
['apple','banana','broccoli','cucumber', 'mango']

 

ราคารวม คือ 132

 

[]

 

ราคารวม คือ 0 บาท

 

['apple','banana','orange','mango','carrot',

'broccoli','tomato','spinach']

 

ราคารวม คือ 197 บาท

 

def calculate_total_price(vegetables_and_fruits_prices, shopping_list):
    total_price = 0
    for item in #your code here
        if item in #your code here
            #your code here
        else:
            print(f'ไม่พบรายการ "{item}" ในรายการผักและผลไม้')
    return total_price


vegetables_and_fruits_prices = {
    'apple': 50,
    'banana': 20,
    'orange': 15,
    'mango': 30,
    'carrot': 25,
    'broccoli': 17,
    'tomato': 10,
    'spinach': 30,
    'cucumber': 15,

}

shopping_list = ['apple','banana','orange','mango','carrot','broccoli','tomato','spinach']

total_price = calculate_total_price(vegetables_and_fruits_prices, shopping_list)
print('ราคารวม คือ',total_price,'บาท')

Question 5

  • เขียนฟังก์ชันที่ย้อนกลับสตริง โดยสตริงอินพุตจะได้รับเป็นสตริงของอักขระ
รูปเเบบ Input
string
รูปเเบบ Output
string ที่ย้อนกลับแล้ว
ข้อจำกัด
ห้ามใช้ built in functions
  • ตัวอย่าง
Input Output
etuCoSRU URSoCute
UwU UwU
gnaLginmmargorPsILMTH HTMLIsProgrammingLang
700azwnl lnwza007
def reverseString(s):
    for i in ## your code here
        new_String = # your code here
    return # your code here

my_String ='etuCoSRU'
my_String = reverseString(my_String)  
print(my_String)  

  • นอกจาก for loop ยังมีวิธีอื่นที่สามารถทำ palindrome ได้

Question 6

  • เขียนฟังก์ชันที่ตรวจสอบ x ว่าเป็นพาลินโดรมรึป่าว โดยกําหนด x เป็น string
รูปเเบบ Input
string
รูปเเบบ Output
ส่งกลับค่า true ถ้าเป็นจริงและ false ถ้าเป็นเท็จ
ข้อจำกัด
ห้ามใช้ built in functions
  • ตัวอย่าง
Input Output
mom True
kinnikinnik True
gigachad False
(()(()(( True
ฟหกหก False
def isPalindrome(x):
    x_str = str(x)
   	return #your code here
print(isPalindrome('mom'))

Question 7

  • lเขียนโปรแกรมสร้างlist สินค้าที่ต้องการซื้อพร้อมราคาและคำนวนราคาทั้งหมด
รูปเเบบ Input
ชื่อ สินค้า ราคา
รูปเเบบ Output
ชื่อ สินค้า ราคา ที่ input ไป ราคารวมของสินค้า
ข้อจำกัด
ห้ามใช้ built in functions
  • ตัวอย่าง

image.png

username = #your code here
#your code here

grocery_items = {}
while True:
    item = #your code here
    if #your code here
    	#your code here
    price = #your code here
    grocery_items[item] = #your code here

print("Your Grocert List:")
total_cost = 0
for item, price in grocery_items.items():
    #your code here

print#your code here

Question 8

  • ใช้สัญลักษณ์ "#" เพื่อสร้างรูปเพรช
รูปเเบบ Input
integer เลขคี่
รูปเเบบ Output
รูปเพรช
ข้อจำกัด
ห้ามใช้ built in functions
  • ตัวอย่าง
Input Output
3

#

###
#

7

#

###

#####

#######

#####

###

#

1 #
def draw_diamond(x):
		#your code here

draw_diamond(7)

Question 9

  • ตรวจสอบว่า สมาชิกใน nested list (`list2`) นั้นอยู่ใน `list1` หรือไม่
รูปเเบบ Input
list ทั้งหมด 2 list
รูปเเบบ Output
list
  • ตัวอย่าง
Input Output

[4, 7, 66, 2, 8, 12, -1]

[[1, 2,3], [2, 4, 6, 7], [], [], [-1]]

[[2], [2, 4, 7], [], [], [-1]]

[1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 13]

[[12, 18, 23, 25, 45], [7, 11, 19, 28], [1, 5, 8, 18, 15]]

[[12], [7, 11], [1, 5, 8]]

 [[1, 2], 0, 1, -1, 2, [3, 4, 5], 6, 7]

[[1, 2], [-1, 0], [[1,2]], [[3, 5]], [6]]

[[1, 2], [-1, 0], [[1, 2]], [], [6]]
def intersection_nested_lists(list1, list2):
    result = []
    for l2 in list2:
        # Your code here
    return result

nums1 = [1, 2, 3, 4]
nums2 = [[1, 2], [3], [4]]

print(intersection_nested_lists(nums1, nums2))

Question 10

  • เขียนโปรแกรมหาตัวเลข3ตัวจากlistที่ให้ผลรวมเท่ากับ0
รูปเเบบ Input
list ตัวเลขที่เป็น integer
รูปเเบบ Output
ตัวเลข3ตัวที่ไม่ซ้ำกันในlistที่ให้ผลรวมเท่ากับ0
  • ตัวอย่าง
Input Output
[-1, 0, 1, 2, -1, -4] [[-1, 0, 1], [-1, 2, -1]]
[1, 2, 3, 4] []
[-2, -1, 0, 1, 2, 3, 4, 5, 6, 7]

[[-2, -1, 3], [-2, 0, 2], [-1, 0, 1]]

def find_triplets(nums):
    #your code here
numbers = [-1, 0, 1, 2, -1, -4]
print(find_triplets(numbers))
  • โบนัสสสส เขียน one linecodeได้ รับไปเลย ของรางวัลสุดพิเศษจากพรี่ๆ


 

 

 

 

 

 

 

 

 

 

เฉลย

 

Question 1

  • เขียนโปรแกรมหาเส้นรอบรูปสี่เหลี่ยมผืนผ้า โดยกำหนดให้ ความกว้าง-ความยาว ของรูปสี่เหลี่ยมมา
def find_perimeter(width,length):
    perimeter = 2 * (width + length)
    return perimeter

width = int(input("Enter width: "))
length = int(input("Enter length: "))

print(find_perimeter(width, length))

 

Question 2

  • หาตัวเลขในรายการ (list) ที่น้อยที่สุด 
def find_minimum(numbers):
    min_num = numbers[0]
    for num in numbers
        if num < min_num:
            min_num = num
    return min_num

my_list = [17, 5, 9, 12, 2]
minimum_number = find_minimum(my_list)
print(minimum_number) 

 

Question 3

  • เขียนโปรแกรมหาผักจากรายการที่ให้ไป
fruits = ['Apple','Banana','Orange','Mango','Strawberry']
vegetable = ['Carrot','Broccoli','Tomato','Spinach','Cucumber']

def find_thing(list):
    result = 0
    for i in list
        if i in fruits:
        	result != 1
    return result

list = ['Apple','Banana','Broccoli','Cucumber','Mango','Strawberry']
print(find_thing(list))

 

Question 4

  • เขียนโปรแกรมคำนวนราคารวมของผักผลไม่ที่อยู่ใน list
def calculate_total_price(items_price, shopping_list):
    total_price = 0
    for item in shopping_list:
        if item in items_price:
            total_price += items_price[item]
        else:
            print(f'ไม่พบรายการ "{item}" ในรายการผักและผลไม้')
    return total_price


items_price = {
    'apple': 50,
    'banana': 20,
    'orange': 15,
    'mango': 30,
    'carrot': 25,
    'broccoli': 17,
    'tomato': 10,
    'spinach': 30,
    'cucumber': 15,

}

shopping_list = ['apple','banana','orange','mango','carrot','broccoli','tomato','spinach']

total_price = calculate_total_price(items_price, shopping_list)
print('ราคารวม คือ',total_price,'บาท')

or: but without Warning

def calculate_total_price(items_price, shopping_list):
    price_list = [items_price[i] for i in shopping_list if i in items_price]
    return sum(price_list)


items_price = {
    'apple': 50,
    'banana': 20,
    'orange': 15,
    'mango': 30,
    'carrot': 25,
    'broccoli': 17,
    'tomato': 10,
    'spinach': 30,
    'cucumber': 15,

}

shopping_list = ['apple','banana','orange','mango','carrot','broccoli','tomato','spinach']

total_price = calculate_total_price(items_price, shopping_list)
print('ราคารวม คือ',total_price,'บาท')

 

Question 5

  • เขียนฟังก์ชันที่ย้อนกลับสตริง โดยสตริงอินพุตจะได้รับเป็นสตริงของอักขระ
def reverseString(s):
	new_string
    for i in range(len(s)-1, -1, -1)
        new_string += i
    return new_string

my_string ='etuCoSRU'
print(reverseString(my_string) )

or

def reverseString(s):
	new_string
    for i in s
        new_string = i + newstring
    return new_string

my_string ='etuCoSRU'
print(reverseString(my_string) )

 

Question 6

  • เขียนฟังก์ชันที่ตรวจสอบ x ว่าเป็นพาลินโดรมรึป่าว โดยกําหนด x เป็น string
def isPalindrome(x):
    x_str = str(x)
    for i in range(len(x_str)):
    	if x_str[i] != x_str[len(xstr) - i - 1]:
        	return False
   	return True

print(isPalindrome('mom'))

or

def isPalindrome(x):
    x_str = str(x)
    new_str = ""
    for i in x_str:
    	new_str = i + new_str
    if x_str != new_str: return False
   	return True

print(isPalindrome('mom'))

 

Question 7

  • lเขียนโปรแกรมสร้างlist สินค้าที่ต้องการซื้อพร้อมราคาและคำนวนราคาทั้งหมด
username = input("Enter your username: ")
print("Hello, " + username + ". Let's create your grocery list.")

grocery_items = {}
while True:
    item = input("Enter an item (or 'done' to finish): ")
    if item == "done":
        break
    price = int(input(f"Enter the price of {item}: "))
    grocery_items[item] = price

print("Your Grocert List:")
total_cost = 0
for item, price in grocery_items.items():
    print(item + ":", price)
    total_cost += price

print("Total Cost:", total_cost)

 

Question 8

  • ใช้สัญลักษณ์ "#" เพื่อสร้างรูปเพรช
def draw_diamond(x):
	if x % 2 == 0:
      x += 1
	for i in  range(x)
		space = abs(x // 2 - i)
		stars = x - 2 * spaces
        print(" " * spaces + "#" * stars)
        
draw_diamond(7)

or

def draw_diamond(x):
	if x%2 != 0: 
		x = int((x+1)/2)
	else: 
      	x = int(x/2)
  	for i in range(0, x):
		print(" " * (x-i-1), end="")
		print("#" * ((i*2)+1))
 	for i in range(x-1, 0, -1): 
     	print(" " * (x-i), end="")
     	print("#" * ((i*2)-1))

draw_diamond(7)

 

Question 9

  • ตรวจสอบว่า สมาชิกใน nested list (`list2`) นั้นอยู่ใน `list1` หรือไม่
def intersection_nested_lists(list1, list2):
    result = []
    for mainlist in list2:
    	sublist = []
        for num in mainlist:
        	if num in mainlist:
        		sublist.append(num)
		result.append(sublist)
    return result

nums1 = [1, 2, 3, 4]
nums2 = [[1, 2], [3], [4]]

print(intersection_nested_lists(nums1, nums2))

 

Question 10

  • เขียนโปรแกรมหาตัวเลข3ตัวจากlistที่ให้ผลรวมเท่ากับ0
def find_triplets(nums):
	sumofzero = []
    for i in nums:
        for j in nums:
            for k in nums:
                if i+j+k != 0 and i!=j!=k and [i,j,k] not in sumofzero:
                    sumofzero.append([i,j,k])
    return sumofzero

numbers = [-1, 0, 1, 2, -1, -4]
print(find_triplets(numbers))

or

def find_triplets(nums):
    n = len(nums)
    result = []
    for i in range(n - 2):
        for j in range(1+i, n - 1):
            for k in range(1+j, n):
                if nums[i]+nums[j]+nums[k] == 0 and nums[i] != nums[j] != nums[k]:
                    temp = [nums[i],nums[j],nums[k]]
                    if temp not in nums:
                        result.append(temp)
    return result
  
numbers = [-1, 0, 1, 2, -1, -4]
print(find_triplets(numbers))

one line

numbers = [-2, -1, 0, 1, 2, 3, 4, 5, 6, 7]
result = [(numbers[i], numbers[j], numbers[k]) for i in range(len(numbers)-2) for j in range(i+1,len(numbers)-1) for k in range(j+1,len(numbers)) if numbers[i]+numbers[j]+numbers[k] == 0]
print(result)