update,
This commit is contained in:
58
saralai19981321/task1/Q3/main.py
Normal file
58
saralai19981321/task1/Q3/main.py
Normal file
@@ -0,0 +1,58 @@
|
||||
#!/usr/bin/env python
|
||||
import os,sys
|
||||
from pprint import pprint
|
||||
from math import sin, pi
|
||||
import random
|
||||
|
||||
|
||||
hours = 24
|
||||
grid_num = 9
|
||||
base_temperature=20
|
||||
temperature_sections = []
|
||||
|
||||
|
||||
def simulateTemperatureChange(hr_of_day, change_cofficient =1):
|
||||
# 3. Use if-else statements to apply the temperature changes based on the time of day.
|
||||
# morning (6-11)
|
||||
# afternoon (12-17)
|
||||
# evening (18-22)
|
||||
# night (23-5)
|
||||
if (hr_of_day >= 6 and hr_of_day <= 11):
|
||||
change_cofficient = -1
|
||||
elif (hr_of_day > 11 and hr_of_day <= 17):
|
||||
change_cofficient = 3
|
||||
elif (hr_of_day > 17 and hr_of_day <= 22):
|
||||
change_cofficient = 2
|
||||
else:
|
||||
change_cofficient = 1
|
||||
|
||||
return base_temperature + (random.uniform(-change_cofficient ,change_cofficient) * sin(hr_of_day/24.0* pi))
|
||||
|
||||
def initRoomTemperature():
|
||||
# 1. Initialize the temperature of the room.
|
||||
return [[0]*grid_num]*hours
|
||||
|
||||
# 2. Use nested loops to update the temperature of each section of the room at each hour.
|
||||
def updateTemperature():
|
||||
# 5. Use a while loop to simulate the temperature changes over a 24-hour period.
|
||||
hr_of_day = 0
|
||||
while hr_of_day < hours:
|
||||
for section in range(grid_num):
|
||||
temperature_sections[hr_of_day][section] = simulateTemperatureChange(hr_of_day)
|
||||
hr_of_day += 1
|
||||
|
||||
# 4. Print the temperature distribution in the room at each hour.
|
||||
def printTemperatureOfRooms():
|
||||
for hr_of_day in range(hours):
|
||||
print(f'hour {hr_of_day+1}')
|
||||
for section in range(0,grid_num,3):
|
||||
print(f'{temperature_sections[hr_of_day][section]:.1f}', end=' ')
|
||||
print(f'{temperature_sections[hr_of_day][section+1]:.1f}', end=' ')
|
||||
print(f'{temperature_sections[hr_of_day][section+2]:.1f}', end=' ')
|
||||
print()
|
||||
print('')
|
||||
temperature_sections=initRoomTemperature()
|
||||
updateTemperature()
|
||||
printTemperatureOfRooms()
|
||||
|
||||
|
61
saralai19981321/task1/Q3/notes.md
Normal file
61
saralai19981321/task1/Q3/notes.md
Normal file
@@ -0,0 +1,61 @@
|
||||
# Question 3: Simulating the Daily Temperature Changes in a Room
|
||||
|
||||
## Scenario:
|
||||
|
||||
Imagine a room where the temperature changes throughout the day. The room is divided into a 3x3 grid, where each cell represents a section of the room. The temperature in the room starts at a uniform value, but it changes based on the time of day. The goal is to simulate how the temperature changes in the room over a 24-hour period.
|
||||
|
||||
## Problem Statement:
|
||||
|
||||
You are required to write a program that simulates the temperature changes in the room.
|
||||
|
||||
The program should:
|
||||
|
||||
1. Initialize the temperature of the room.
|
||||
2. Use nested loops to update the temperature of each section of the room at each hour.
|
||||
3. Use if-else statements to apply the temperature changes based on the time of day.
|
||||
4. Print the temperature distribution in the room at each hour.
|
||||
5. Use a while loop to simulate the temperature changes over a 24-hour period.
|
||||
|
||||
## Assumptions:
|
||||
|
||||
- The room is a 3x3 grid.
|
||||
- The initial temperature of the room is 20°C.
|
||||
- The temperature increases by 1°C every hour from 6 AM to 6 PM.
|
||||
- The temperature decreases by 1°C every hour from 6 PM to 6 AM.
|
||||
- The simulation runs for 24 hours.
|
||||
|
||||
## Instructions (for reference):
|
||||
|
||||
1. Initialize a 3x3 grid representing the temperature of the room.
|
||||
2. Use nested loops to iterate through each section of the room.
|
||||
3. Use if-else statements to apply the temperature changes based on the time of day.
|
||||
4. Update the temperature of each section in the grid.
|
||||
5. Print the temperature distribution in the room at each hour.
|
||||
6. Use a while loop to simulate the temperature changes over 24 hours.
|
||||
|
||||
## Deliverables:
|
||||
|
||||
- A well-commented code that simulates the temperature changes in the room.
|
||||
- A brief explanation of how the code works.
|
||||
|
||||
### Sample Output:
|
||||
|
||||
Enter the Room Temperature: 30
|
||||
Enter the Number of Hours between 0-24: 3
|
||||
|
||||
```
|
||||
Hour 0
|
||||
29.0 29.0 29.0
|
||||
29.0 29.0 29.0
|
||||
29.0 29.0 29.0
|
||||
|
||||
Hour 1
|
||||
28.0 28.0 28.0
|
||||
28.0 28.0 28.0
|
||||
28.0 28.0 28.0
|
||||
|
||||
Hour 2
|
||||
27.0 27.0 27.0
|
||||
27.0 27.0 27.0
|
||||
27.0 27.0 27.0
|
||||
```
|
Reference in New Issue
Block a user