Write a Python program to print alphabet pattern 'Z. Expected Output: ********** * ¥ * * * * * * **********
时间: 2024-03-18 17:37:18 浏览: 243
以下是一个Python程序,用于打印字母模式'Z':
```python
def print_z_pattern():
for i in range(10):
if i == 0 or i == 9:
print("**********")
else:
print(" *")
print_z_pattern()
```
预期输出:
```
**********
*
*
*
*
*
*
*
*
**********
```
相关问题
Read the stock price data of Alphabet Inc. (GOOG) for the time period between April 01, 2020 and October 01, 2020 in the file 'alphabet.csv' into a Pandas DataFrame. You can download it from Blackboard in session 12. (1) Convert the “Date” column into Datetime format and assign it as the index column. And output the DataFram containing data for month x. (2) Calculate the net increase of stock price for each day and append them as a new column “Net Increase” to the DataFrame using the closing price minus the opening price. And output the DataFram containing data for month x. (3) Find the day that has the highest opening price, and calculate the mean of closing price for all days. NOTE:You should add the following code to your program to ensure that all columns are displayed when printing.Otherwise, the system will consider it as incorrect. pd.set_option('display.max_columns',1000) pd.set_option('display.width', 1000) pd.set_option('display.max_colwidth',1000) Input An integer x. Output Task1: A DataFram containing data. Task2: A DataFram containing data. Task3: A datetime object. A float number(Keep six decimal places.).
Task1:
```python
import pandas as pd
# read in the data
df = pd.read_csv('alphabet.csv')
# convert the 'Date' column to datetime format and set it as the index
df['Date'] = pd.to_datetime(df['Date'])
df.set_index('Date', inplace=True)
# select data for month x (assuming x is an integer between 1 and 12)
x = 4
df_month_x = df[df.index.month == x]
# print the DataFrame
pd.set_option('display.max_columns', 1000)
pd.set_option('display.width', 1000)
pd.set_option('display.max_colwidth', 1000)
print(df_month_x)
```
Task2:
```python
# calculate the net increase of stock price for each day and append it as a new column
df['Net Increase'] = df['Close'] - df['Open']
# select data for month x (assuming x is an integer between 1 and 12)
x = 4
df_month_x = df[df.index.month == x]
# print the DataFrame
pd.set_option('display.max_columns', 1000)
pd.set_option('display.width', 1000)
pd.set_option('display.max_colwidth', 1000)
print(df_month_x)
```
Task3:
```python
# find the day with the highest opening price
max_open = df['Open'].max()
max_open_date = df[df['Open'] == max_open].index[0]
# calculate the mean of closing price for all days
mean_close = df['Close'].mean()
# print the results
print(max_open_date)
print(round(mean_close, 6))
```
You are required to write a C program to: • Initialize GPIO peripherals • Initialise UART peripheral for receiving ASCII characters ‘A’ to ‘Z’ at baud 9600 • Initialise an internal array to hold 10 characters with head and tail: CharBuff • Repeat the following:o When data is received on the serial communication port, read ASCII character X, o If received character X is a capital letter add it to CharBuff, else ignore. o While CharBuff is not empty, transmit the morse code of the oldest stored character by blinking the LED (code provided for you). o When CharBuff is full, disable UART RX. o If UART RX is disabled, pushing the button P_B1 will activate it; otherwise, pushing the button does not affect your programme. You are recommended to use interrupt to control UART receiving data and coordinate the operation between CharBuff and P_LD2. 在我的代码基础上完成以上任务#include <platform.h> #include <gpio.h> #include "delay.h" #include "uart.h" #include <stm32f4xx.h> /* NOTE******** YOU CAN USE THE IN-UILT FUNCTION delay_ms(HOW_LONG) TO CAUSE A DELAY OF HOW_LONG MILLI SECONDS
由于您的要求涉及到硬件操作和中断控制,需要在具体的嵌入式系统上进行编程。下面是一个基于 STM32F4xx 的 C 代码示例,可以实现您所要求的功能:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "stm32f4xx.h"
#include "stm32f4xx_gpio.h"
#include "stm32f4xx_usart.h"
#define BUFFER_SIZE 10
GPIO_InitTypeDef GPIO_InitStruct;
USART_InitTypeDef USART_InitStruct;
NVIC_InitTypeDef NVIC_InitStruct;
volatile char CharBuff[BUFFER_SIZE];
volatile int head = 0;
volatile int tail = 0;
volatile int buffer_full = 0;
volatile int uart_rx_enabled = 1;
void init_gpio(void) {
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_5;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOA, &GPIO_InitStruct);
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_DOWN;
GPIO_Init(GPIOA, &GPIO_InitStruct);
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOA, &GPIO_InitStruct);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_USART2);
}
void init_uart(void) {
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
USART_InitStruct.USART_BaudRate = 9600;
USART_InitStruct.USART_WordLength = USART_WordLength_8b;
USART_InitStruct.USART_StopBits = USART_StopBits_1;
USART_InitStruct.USART_Parity = USART_Parity_No;
USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStruct.USART_Mode = USART_Mode_Rx;
USART_Init(USART2, &USART_InitStruct);
NVIC_InitStruct.NVIC_IRQChannel = USART2_IRQn;
NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStruct.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStruct);
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
USART_Cmd(USART2, ENABLE);
}
void USART2_IRQHandler(void) {
if (USART_GetITStatus(USART2, USART_IT_RXNE) != RESET) {
char data = USART_ReceiveData(USART2);
if (data >= 'A' && data <= 'Z' && !buffer_full && uart_rx_enabled) {
CharBuff[tail] = data;
tail = (tail + 1) % BUFFER_SIZE;
if (tail == head) {
buffer_full = 1;
USART_ITConfig(USART2, USART_IT_RXNE, DISABLE);
}
}
USART_ClearITPendingBit(USART2, USART_IT_RXNE);
}
}
void blink_led(char data) {
int i, j;
int dot_time = 200;
int dash_time = 500;
switch (data) {
case 'A':
GPIO_SetBits(GPIOA, GPIO_Pin_5);
delay_ms(dot_time);
GPIO_ResetBits(GPIOA, GPIO_Pin_5);
delay_ms(dash_time);
GPIO_SetBits(GPIOA, GPIO_Pin_5);
delay_ms(dot_time);
GPIO_ResetBits(GPIOA, GPIO_Pin_5);
break;
case 'B':
GPIO_SetBits(GPIOA, GPIO_Pin_5);
delay_ms(dash_time);
GPIO_ResetBits(GPIOA, GPIO_Pin_5);
delay_ms(dot_time);
GPIO_SetBits(GPIOA, GPIO_Pin_5);
delay_ms(dot_time);
GPIO_ResetBits(GPIOA, GPIO_Pin_5);
delay_ms(dot_time);
GPIO_SetBits(GPIOA, GPIO_Pin_5);
delay_ms(dot_time);
GPIO_ResetBits(GPIOA, GPIO_Pin_5);
break;
// Add more cases for the rest of the alphabet...
}
}
int main(void) {
init_gpio();
init_uart();
while (1) {
if (head != tail) {
blink_led(CharBuff[head]);
head = (head + 1) % BUFFER_SIZE;
if (buffer_full) {
buffer_full = 0;
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
}
}
if (!UART_RX_ENABLED && GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_0)) {
UART_RX_ENABLED = 1;
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
}
}
}
```
该程序使用 STM32F4xx 的 GPIO 和 USART 模块来实现所需的功能。其中,`init_gpio()` 函数用于初始化 GPIO 端口,`init_uart()` 函数用于初始化 USART 端口。在中断服务函数 `USART2_IRQHandler()` 中,程序会检测是否有数据通过 USART 接收到,如果是大写字母且 CharBuff 没有满,那么将其添加到 CharBuff 中。如果 CharBuff 已满,则禁用 UART RX 中断。在主循环中,程序会周期性地检查 CharBuff 是否有数据,如果有则将其转换为 Morse 代码并通过 LED 闪烁输出。当 CharBuff 已满时,程序会禁用 UART RX 中断,直到 CharBuff 又有空间可用。同时,程序会检测 P_B1 按键是否按下,如果按下且 UART RX 已禁用,则启用 UART RX 中断。
阅读全文
相关推荐
![](https://img-home.csdnimg.cn/images/20250102104920.png)
![pptx](https://img-home.csdnimg.cn/images/20241231044947.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)