All experience with string

本文介绍了一种将字符串转换为数值类型的通用方法,并提供了一个C++模板函数实现。该函数能够根据指定的进制(十进制、十六进制等)进行转换。
1.Convert string to numeric
#include <string>
#include <sstream>
#include <iostream>

// the third parameter of from_string() should be 
// one of std::hex, std::dec or std::oct
template <class T>
bool from_string(T& t,
                 const std::string& s,
                 std::ios_base& (*f)(std::ios_base&))  

   

A study was conducted, in which participants, between 18 and 99 years old, were asked to complete a task. Each participant received a score between 0 and 100. The scores that the participants achieved in the task were recorded, as well as their first names, last names, ages, genders, and amount of prior experience. Approximately 10,000 people participated in the study. Unfortunately, a few mistakes were made in recording the data: Some participants’ information was recorded in the database multiple times. Some participants did not specify their gender leaving it blank instead of as ‘Male’, ‘Female’, or ‘Other’. Some participants specified their prior experience on a scale of 1 to 10 instead of 1 to 5 as instructed. For some participants, their scores were corrupted and replaced with NaN. In this assignment, you will practice data cleaning. The input is a file named ‘input.csv’ (see reading materials), which will be read and saved as a table variable ‘T’. You will write a function called ‘clean_table’. This function will take the table variable ‘T’ as input and handle the following data cleaning tasks: Remove duplicate rows. Replace NaN values in the score column with the mean score of other participants. Replace any empty string in the gender column with ‘unspecified’. For participants who entered prior experience using a 1-to-10 scale instead of the required 1-to-5 scale, divide their experience by 2 and round it down using the floor function. Function function T2 = clean_table(table) % Remove duplicated rows. Assign the table to T2. You may refer to the warm-up exercise. % Your code here: % Find the indexes of scores with a value of ‘NaN’; Assign them to zero; Use the same way of the warm-up exercise to calculate the mean score; Assign the mean value to those with ‘NaN’. % Your code here: % You may use the fillmissing function. Here it is. You may explain it. T2 = fillmissing(T2, ‘constant’, ‘unspecified’, ‘DataVariables’, {‘Gender’}); % Access those with prior experience value >5 and floor them. You may use a similar way compared to the warm-up exercise. % Your code here: end code to call your function T = readtable(“input.csv”); clean_table(T);
最新发布
09-10
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值