Make a program that filters a list of strings and returns a list with only your friends name in it. If a name has exactly 4 letters in it, you can be sure that it has to be a friend of yours! Otherwise, you can be sure he's not... Ex: Input = ["Ryan", "Kieran", "Jason", "Yous"], Output = ["Ryan", "Yous"]
时间: 2024-03-31 19:33:55 浏览: 406
Sure, I can help you with that! Here's a Python program that filters a list of strings and returns only your friends' names:
```
def filter_friends(names):
friends = []
for name in names:
if len(name) == 4:
friends.append(name)
return friends
```
You can use this function by passing in a list of names as an argument, like this:
```
names = ["Ryan", "Kieran", "Jason", "Yous"]
friends = filter_friends(names)
print(friends)
```
The output will be:
```
["Ryan", "Yous"]
```
This is because "Ryan" and "Yous" both have exactly 4 letters, so they are your friends! "Kieran" and "Jason" have more than 4 letters, so they are not your friends.
相关问题
Make a JavaScript program that filters a list of strings and returns a list with only your friends name in it. If a name has exactly 4 letters in it, you can be sure that it has to be a friend of yours! Otherwise, you can be sure he's not... Ex: Input = ["Ryan", "Kieran", "Jason", "Yous"], Output = ["Ryan", "Yous"]
Of course, here's a JavaScript program that filters a list of strings and returns only your friends' names:
```
function filterFriends(names) {
let friends = [];
for (let i = 0; i < names.length; i++) {
if (names[i].length === 4) {
friends.push(names[i]);
}
}
return friends;
}
```
You can use this function by passing in a list of names as an argument, like this:
```
let names = ["Ryan", "Kieran", "Jason", "Yous"];
let friends = filterFriends(names);
console.log(friends);
```
The output will be:
```
["Ryan", "Yous"]
```
This is because "Ryan" and "Yous" both have exactly 4 letters, so they are your friends! "Kieran" and "Jason" have more than 4 letters, so they are not your friends.
阅读全文