面试题答案
一键面试def check_condition(num, str, bool_val):
return num > 10 and len(str) > 5 and bool_val
def main():
test1_result = check_condition(15, "hello world", True)
test2_result = check_condition(5, "hello", False)
test3_result = check_condition(12, "longer string", True)
print(f"Test 1 result: {test1_result}")
print(f"Test 2 result: {test2_result}")
print(f"Test 3 result: {test3_result}")
if __name__ == "__main__":
main()
class Main {
static boolean check_condition(int num, String str, boolean bool_val) {
return num > 10 && str.length() > 5 && bool_val;
}
public static void main(String[] args) {
boolean test1_result = check_condition(15, "hello world", true);
boolean test2_result = check_condition(5, "hello", false);
boolean test3_result = check_condition(12, "longer string", true);
System.out.println("Test 1 result: " + test1_result);
System.out.println("Test 2 result: " + test2_result);
System.out.println("Test 3 result: " + test3_result);
}
}
#include <iostream>
#include <string>
bool check_condition(int num, const std::string& str, bool bool_val) {
return num > 10 && str.length() > 5 && bool_val;
}
int main() {
bool test1_result = check_condition(15, "hello world", true);
bool test2_result = check_condition(5, "hello", false);
bool test3_result = check_condition(12, "longer string", true);
std::cout << "Test 1 result: " << (test1_result? "true" : "false") << std::endl;
std::cout << "Test 2 result: " << (test2_result? "true" : "false") << std::endl;
std::cout << "Test 3 result: " << (test3_result? "true" : "false") << std::endl;
return 0;
}
以上分别给出了Python、Java、C++三种语言实现该功能的代码,以满足不同语言的面试要求。
- Python代码:
check_condition
函数通过逻辑与(and
)连接三个条件判断是否满足要求。main
函数调用check_condition
函数并传入不同参数进行测试,最后打印测试结果。
- Java代码:
check_condition
方法同样使用逻辑与(&&
)连接条件判断。main
方法进行测试并打印结果。
- C++代码:
check_condition
函数使用逻辑与(&&
)判断条件。main
函数进行测试,通过条件运算符将bool
值转换为字符串输出。