查找字符个数最长的单词

时间:2018-09-09 08:15:52

标签: r text-processing

所以我有一个字符串,我需要找到匹配两个约束的单词,即单词中的字符数应该是偶数,并且应该是最长的单词。

例如:

Input: I am a bad coder with good logical skills
Output: skills

只需从R开始,那么任何帮助都会很棒。

2 个答案:

答案 0 :(得分:2)

您可以尝试使用库import React, { Component } from "react"; import axios from "axios"; import moment from "moment"; export default class Feutred extends Component { state = { sports: [], events: [], isLoading: true, errors: null }; getSports() { axios .get("/api/v1/sports.json") .then(response => response.data.map(sport => ({ id: sport.id, name: sport.name, slug: sport.slug })) ) .then(sports => { this.setState({ sports, isLoading: false }); }) .catch(error => this.setState({ error, isLoading: false })); } getEvents() { axios .get("/api/v1/events?sport_id=${sport_id}") .then(response => response.data.map(event => ({ id: event.id, time: event.time, league: event.league, time_status: event.time_status, homeTeam: event.home, awayTeam: event.away })) ) .then(events => { this.setState({ events, isLoading: false }); }) .catch(error => this.setState({ error, isLoading: false })); } componentDidMount() { this.getSports(); (this.interval = setInterval( () => this.getEvents({ time: Date.now() }), 12000 )); } componentWillUnmount() { clearInterval(this.interval); } render() { const { sports, isLoading } = this.state; return ( <React.Fragment> {!isLoading ? ( sports.map(sport => { const { id, name } = sport; return ( <div key={sport.id}> <div className="text"> <p className="meta"> <span className="matchinfo"> <span className="block">time</span> <span className="block">timestatus</span> </span> </p> <h3> <a href="">home-team vs aya tream</a> </h3> <p className="league"> <a className="watchlive" href=""> <span className="icon" /> <span>Watch live</span> </a> <span>{sport.name} - league cc - league name</span> </p> </div> </div> ); }) ) : ( <p>Loading...</p> )} </React.Fragment> ); } }

tokenizers

答案 1 :(得分:0)

这是我的代码:

input<-"I am a bad coder with good logical skills"

words<-strsplit(input," ")                                      # Split it to words

countWords<-sapply(words,nchar)                                 # Count the length of words

dt<-data.frame( word=unlist(words), length=unlist(countWords) ) # Make a dataframe

dt<-dt[order(dt$length),]                                       # Sort the dataframe based on length

dt<-dt[  which((dt$length %% 2)==1),]                           # Get the words with odd length

dt[nrow(dt),]                                                   # Get the longest word