【yukicoder】No.729 文字swap

 

使用言語

 

Python 3

 

問題文

 

No.729 文字swap - yukicoder

 

 

解法プロセス

 

1、入力した文字列をリストに分割
2、入れ替えたい要素(文字)を逆にする

 

回答

 

s = list(input())
i,j = map(int, input().split())
s[i],s[j] = s[j],s[i]
print(''.join(s))

 

【yukicoder】No.725 木は明らかに森である

 

使用言語

 

Python 3

 

問題文

 

No.725 木は明らかに森である - yukicoder

 

解法プロセス

 

1、入力値の中に’treeone’があるかを調べる
2、もしあれば、’forest’に置き換える
3、メソッド、replace(変更前, 変更後)を使用

 

回答

 

s = input()
if 'treeone' in s: 
    print(s.replace('treeone', 'forest'))
else:
    print(s)

 

【yukicoder】No.739 大事なことなので2回言います

 

使用言語

 

Python 3

 

問題文

 

No.739 大事なことなので2度言います - yukicoder

 

解法プロセス

 

1、2つの同じ文字列が連続してるなら、文字数は偶数になるはず
2、なので、入力値の文字数が2で割り切れるかを調べる
3、同じ文字列なら、中央から左と右のそれぞれの文字数は同じになる
(’oneone’ならone:3文字, one:3文字、となる)

 

回答

 

s1 = input()

if len(s1) % 2 != 0:
    print('NO')
else:
    if s1[int(len(s1)/2):] == s1[:int(len(s1)/2)]:
        print('YES')
    else:
        print('NO') 

 

【Swift】TableViewでセルの伸び縮みを再現してみた

 

 

解説

 

 ・セルをタップすると、そのセルが縦に拡大する(伸びる)

・1番下のセルを押すと、なぜかそれよりも下の全てのセルも伸び縮みしてしまう

(原因はわかりません。)

・セルをタップした時に表示される色を消すために、tableView.deselectRow、を記述

 

ストーリーボード

 

f:id:nekokichi_yos2:20181011145653p:plain

f:id:nekokichi_yos2:20181011145655p:plain

 

ソースコード1

 

import UIKit

class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {
    
    
    @IBOutlet weak var tableView: UITableView!
    
    let dataSource: [UIColor] = [.red, .green, .blue, .cyan, .yellow]
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        tableView.delegate = self
        tableView.dataSource = self
        
        //カスタムセルをtableViewに登録
        tableView.register(UINib(nibName: "CustomCell",
                                 bundle: nil), forCellReuseIdentifier: "CustomCell")
        
    }
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return dataSource.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCell(withIdentifier: "CustomCell",
                                                 for: indexPath) as! CustomCell
        
        //カスタムセル - contentView - View(subview) - first?(1つ目の)
        //CustomCell.xibでは、contentViewの上にViewが置かれている
        //※cell.backgroundColor = ... はダメだった
        cell.contentView.subviews.first?.backgroundColor = dataSource[indexPath.row]
        
        return cell
        
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        let cell = tableView.cellForRow(at: indexPath) as! CustomCell
        
        tableView.deselectRow(at: indexPath, animated: false)
        
        //セルの伸び縮みアニメーションには、下記が推奨されている
        //beginUpdates(),
        //ここにコードを書く
        //endUpdates()
        tableView.beginUpdates()
        
        //セルの高さを変更する
        cell.cellheight()
        
        tableView.endUpdates()
        
    }

}

 

ソースコード2

 

import UIKit

class CustomCell: UITableViewCell {
    
    @IBOutlet weak var cellHeight: NSLayoutConstraint!
    
    //セルの高さを変更する
    func cellheight() {
        //heightのlayerに条件式で代入
        //44ですか? Yes/100、No/44
        cellHeight.constant = cellHeight.constant == 44 ? 100 : 44
    }
    
}