Best Rod code snippet using rod.trySlowmotion
element.go
Source:element.go
...50}51// ScrollIntoView å°å½åå
ç´ æ»å¨å°æµè§å¨çªå£çå¯è§åºåä¸ï¼å¦æå®å°æªå¨å¯è§åºåå
ï¼ã52func (el *Element) ScrollIntoView() error {53 defer el.tryTrace(TraceTypeInput, "scroll into view")()54 el.page.browser.trySlowmotion()55 err := el.WaitStableRAF()56 if err != nil {57 return err58 }59 return proto.DOMScrollIntoViewIfNeeded{ObjectID: el.id()}.Call(el)60}61// Hover å°é¼ æ åå¨å
ç´ çä¸å¿62// å¨æ§è¡è¯¥æä½ä¹åï¼å®å°å°è¯æ»å¨å°è¯¥å
ç´ å¹¶çå¾
å
¶å¯äº¤äºã63func (el *Element) Hover() error {64 pt, err := el.WaitInteractable()65 if err != nil {66 return err67 }68 return el.page.Mouse.Move(pt.X, pt.Y, 1)69}70// MoveMouseOut å°é¼ æ 移åºå½åå
ç´ 71func (el *Element) MoveMouseOut() error {72 shape, err := el.Shape()73 if err != nil {74 return err75 }76 box := shape.Box()77 return el.page.Mouse.Move(box.X+box.Width, box.Y, 1)78}79// Click ä¼å人ä¸æ ·æä¸ç¶åéæ¾æé®ã80// å¨æ§è¡æä½ä¹åï¼å®å°å°è¯æ»å¨å°å
ç´ ï¼å°é¼ æ æ¬åå¨è¯¥å
ç´ ä¸ï¼çå¾
该å
ç´ å¯äº¤äºå¹¶å¯ç¨ã81func (el *Element) Click(button proto.InputMouseButton) error {82 err := el.Hover()83 if err != nil {84 return err85 }86 err = el.WaitEnabled()87 if err != nil {88 return err89 }90 defer el.tryTrace(TraceTypeInput, string(button)+" click")()91 return el.page.Mouse.Click(button)92}93// Tap å°æ»å¨å°æé®å¹¶å人类ä¸æ ·ç¹å»å®ã94// å¨æ§è¡æ¤æä½ä¹åï¼å®å°å°è¯æ»å¨å°å
ç´ ï¼å¹¶çå¾
å
¶å¯äº¤äºå¹¶å¯ç¨ã95func (el *Element) Tap() error {96 err := el.ScrollIntoView()97 if err != nil {98 return err99 }100 err = el.WaitEnabled()101 if err != nil {102 return err103 }104 pt, err := el.WaitInteractable()105 if err != nil {106 return err107 }108 defer el.tryTrace(TraceTypeInput, "tap")()109 return el.page.Touch.Tap(pt.X, pt.Y)110}111// Interactable æ£æ¥è¯¥å
ç´ æ¯å¦å¯ä»¥ä¸å
æ 交äºã112// å
æ å¯ä»¥æ¯é¼ æ ãææãæåç¬çã113// å¦æä¸æ¯å¯äº¤äºçï¼Errå°æ¯ErrNotInteractableï¼ä¾å¦å½è¢«ä¸ä¸ªæ¨¡ææ¡è¦çæ¶ã114func (el *Element) Interactable() (pt *proto.Point, err error) {115 noPointerEvents, err := el.Eval(`() => getComputedStyle(this).pointerEvents === 'none'`)116 if err != nil {117 return nil, err118 }119 if noPointerEvents.Value.Bool() {120 return nil, &ErrNoPointerEvents{el}121 }122 shape, err := el.Shape()123 if err != nil {124 return nil, err125 }126 pt = shape.OnePointInside()127 if pt == nil {128 err = &ErrInvisibleShape{el}129 return130 }131 scroll, err := el.page.root.Eval(`() => ({ x: window.scrollX, y: window.scrollY })`)132 if err != nil {133 return134 }135 elAtPoint, err := el.page.ElementFromPoint(136 int(pt.X)+scroll.Value.Get("x").Int(),137 int(pt.Y)+scroll.Value.Get("y").Int(),138 )139 if err != nil {140 if errors.Is(err, cdp.ErrNodeNotFoundAtPos) {141 err = &ErrInvisibleShape{el}142 }143 return144 }145 isParent, err := el.ContainsElement(elAtPoint)146 if err != nil {147 return148 }149 if !isParent {150 err = &ErrCovered{elAtPoint}151 }152 return153}154// Shape DOMå
ç´ å
容çå½¢ç¶ã该形ç¶æ¯ä¸ç»4è¾¹å¤è¾¹å½¢ï¼4è§ï¼ã155// 4-gonä¸ä¸å®æ¯ä¸ä¸ªé¿æ¹å½¢ã4-gonå¯ä»¥å½¼æ¤åå¼ã156// ä¾å¦ï¼æ们使ç¨2个4è§æ¥æ述以ä¸å½¢ç¶ï¼157//158// ____________ ____________159// / ___/ = /___________/ + _________160// /________/ /________/161//162func (el *Element) Shape() (*proto.DOMGetContentQuadsResult, error) {163 return proto.DOMGetContentQuads{ObjectID: el.id()}.Call(el)164}165// Type ä¸Keyboard.Type类似ã166// å¨æ§è¡æä½ä¹åï¼å®å°å°è¯æ»å¨å°è¯¥å
ç´ å¹¶å°ç¦ç¹éä¸å¨è¯¥å
ç´ ä¸ã167func (el *Element) Type(keys ...input.Key) error {168 err := el.Focus()169 if err != nil {170 return err171 }172 return el.page.Keyboard.Type(keys...)173}174// KeyActions ä¸Page.KeyActions类似ã175// å¨æ§è¡æä½ä¹åï¼å®å°å°è¯æ»å¨å°è¯¥å
ç´ å¹¶å°ç¦ç¹éä¸å¨è¯¥å
ç´ ä¸ã176func (el *Element) KeyActions() (*KeyActions, error) {177 err := el.Focus()178 if err != nil {179 return nil, err180 }181 return el.page.KeyActions(), nil182}183// SelectText éæ©ä¸æ£å表达å¼å¹é
çææ¬ã184// å¨æ§è¡æä½ä¹åï¼å®å°å°è¯æ»å¨å°è¯¥å
ç´ å¹¶å°ç¦ç¹éä¸å¨è¯¥å
ç´ ä¸ã185func (el *Element) SelectText(regex string) error {186 err := el.Focus()187 if err != nil {188 return err189 }190 defer el.tryTrace(TraceTypeInput, "select text: "+regex)()191 el.page.browser.trySlowmotion()192 _, err = el.Evaluate(evalHelper(js.SelectText, regex).ByUser())193 return err194}195// SelectAllText éæ©ææææ¬196// å¨æ§è¡æä½ä¹åï¼å®å°å°è¯æ»å¨å°è¯¥å
ç´ å¹¶å°ç¦ç¹éä¸å¨è¯¥å
ç´ ä¸ã197func (el *Element) SelectAllText() error {198 err := el.Focus()199 if err != nil {200 return err201 }202 defer el.tryTrace(TraceTypeInput, "select all text")()203 el.page.browser.trySlowmotion()204 _, err = el.Evaluate(evalHelper(js.SelectAllText).ByUser())205 return err206}207// Input èç¦å¨è¯¥å
ç´ ä¸å¹¶è¾å
¥ææ¬.208// å¨æ§è¡æä½ä¹åï¼å®å°æ»å¨å°å
ç´ ï¼çå¾
å
¶å¯è§ãå¯ç¨åå¯åã209// è¦æ¸
空è¾å
¥ï¼å¯ä»¥ä½¿ç¨el.SelectAllTextï¼ï¼.MustInputï¼ââï¼ä¹ç±»çå½ä»¤210func (el *Element) Input(text string) error {211 err := el.Focus()212 if err != nil {213 return err214 }215 err = el.WaitEnabled()216 if err != nil {217 return err218 }219 err = el.WaitWritable()220 if err != nil {221 return err222 }223 err = el.page.InsertText(text)224 _, _ = el.Evaluate(evalHelper(js.InputEvent).ByUser())225 return err226}227// InputTime èç¦è¯¥å
ç´ åå
¶è¾å
¥æ¶é´ã228// å¨æ§è¡æä½ä¹åï¼å®å°æ»å¨å°å
ç´ ï¼çå¾
å
¶å¯è§ãå¯ç¨åå¯åã229// å®å°çå¾
å
ç´ å¯è§ãå¯ç¨åå¯åã230func (el *Element) InputTime(t time.Time) error {231 err := el.Focus()232 if err != nil {233 return err234 }235 err = el.WaitEnabled()236 if err != nil {237 return err238 }239 err = el.WaitWritable()240 if err != nil {241 return err242 }243 defer el.tryTrace(TraceTypeInput, "input "+t.String())()244 _, err = el.Evaluate(evalHelper(js.InputTime, t.UnixNano()/1e6).ByUser())245 return err246}247// Blur 类似äºæ¹æ³ Blur248func (el *Element) Blur() error {249 _, err := el.Evaluate(Eval("() => this.blur()").ByUser())250 return err251}252// Select éæ©ä¸éæ©å¨å¹é
çåé项å
ç´ ã253// å¨æä½ä¹åï¼å®å°æ»å¨å°å
ç´ ï¼çå¾
å®å¯è§ã254// å¦æ没æä¸éæ©å¨å¹é
çé项ï¼å®å°è¿åErrElementNotFoundã255func (el *Element) Select(selectors []string, selected bool, t SelectorType) error {256 err := el.Focus()257 if err != nil {258 return err259 }260 defer el.tryTrace(TraceTypeInput, fmt.Sprintf(`select "%s"`, strings.Join(selectors, "; ")))()261 el.page.browser.trySlowmotion()262 res, err := el.Evaluate(evalHelper(js.Select, selectors, selected, t).ByUser())263 if err != nil {264 return err265 }266 if !res.Value.Bool() {267 return &ErrElementNotFound{}268 }269 return nil270}271// Matches æ£æ¥csséæ©å¨æ¯å¦å¯ä»¥éæ©å
ç´ 272func (el *Element) Matches(selector string) (bool, error) {273 res, err := el.Eval(`s => this.matches(s)`, selector)274 if err != nil {275 return false, err276 }277 return res.Value.Bool(), nil278}279// Attribute DOM对象çå±æ§280// Attribute vs Property: https://stackoverflow.com/questions/6003819/what-is-the-difference-between-properties-and-attributes-in-html281func (el *Element) Attribute(name string) (*string, error) {282 attr, err := el.Eval("(n) => this.getAttribute(n)", name)283 if err != nil {284 return nil, err285 }286 if attr.Value.Nil() {287 return nil, nil288 }289 s := attr.Value.Str()290 return &s, nil291}292// Property DOM对象çå±æ§293// Property vs Attribute: https://stackoverflow.com/questions/6003819/what-is-the-difference-between-properties-and-attributes-in-html294func (el *Element) Property(name string) (gson.JSON, error) {295 prop, err := el.Eval("(n) => this[n]", name)296 if err != nil {297 return gson.New(nil), err298 }299 return prop.Value, nil300}301// SetFiles 设置å½åæ件è¾å
¥å
ç´ çæ件302func (el *Element) SetFiles(paths []string) error {303 absPaths := []string{}304 for _, p := range paths {305 absPath, err := filepath.Abs(p)306 utils.E(err)307 absPaths = append(absPaths, absPath)308 }309 defer el.tryTrace(TraceTypeInput, fmt.Sprintf("set files: %v", absPaths))()310 el.page.browser.trySlowmotion()311 err := proto.DOMSetFileInputFiles{312 Files: absPaths,313 ObjectID: el.id(),314 }.Call(el)315 return err316}317// Describe æè¿°å½åå
ç´ ã深度æ¯åºæ£ç´¢å级çæ大深度ï¼é»è®¤ä¸º1ï¼å¯¹æ´ä¸ªåæ 使ç¨-1ï¼ææä¾å¤§äº0çæ´æ°ã318// pierceå³å®å¨è¿ååæ æ¶æ¯å¦è¦éåiframesåå½±åæ ¹ã319// è¿åçproto.DOMNodeãNodeIDå°å§ç»ä¸ºç©ºï¼å 为NodeIDä¸ç¨³å®ï¼å½proto.DOMDocumentUpdated被触åæ¶ï¼320// 页é¢ä¸çææNodeIDé½å°è¢«éæ°åé
å°å¦ä¸ä¸ªå¼ï¼ãæ们ä¸å»ºè®®ä½¿ç¨NodeIDï¼èæ¯ä½¿ç¨BackendNodeIDæ¥æ è¯å
ç´ ã321func (el *Element) Describe(depth int, pierce bool) (*proto.DOMNode, error) {322 val, err := proto.DOMDescribeNode{ObjectID: el.id(), Depth: gson.Int(depth), Pierce: pierce}.Call(el)323 if err != nil {324 return nil, err...
input.go
Source:input.go
...48 k.Lock()49 defer k.Unlock()50 for _, key := range keys {51 defer k.page.tryTrace(TraceTypeInput, "press "+input.Keys[key].Key)()52 k.page.browser.trySlowmotion()53 actions := input.Encode(key)54 k.modifiers = actions[0].Modifiers55 defer func() { k.modifiers = 0 }()56 for _, action := range actions {57 err := action.Call(k.page)58 if err != nil {59 return err60 }61 }62 }63 return nil64}65// InsertText is like pasting text into the page66func (k *Keyboard) InsertText(text string) error {67 k.Lock()68 defer k.Unlock()69 defer k.page.tryTrace(TraceTypeInput, "insert text "+text)()70 k.page.browser.trySlowmotion()71 err := proto.InputInsertText{Text: text}.Call(k.page)72 return err73}74// Mouse represents the mouse on a page, it's always related the main frame75type Mouse struct {76 sync.Mutex77 page *Page78 id string // mouse svg dom element id79 x float6480 y float6481 // the buttons is currently beening pressed, reflects the press order82 buttons []proto.InputMouseButton83}84// Move to the absolute position with specified steps85func (m *Mouse) Move(x, y float64, steps int) error {86 m.Lock()87 defer m.Unlock()88 if steps < 1 {89 steps = 190 }91 stepX := (x - m.x) / float64(steps)92 stepY := (y - m.y) / float64(steps)93 button, buttons := input.EncodeMouseButton(m.buttons)94 for i := 0; i < steps; i++ {95 m.page.browser.trySlowmotion()96 toX := m.x + stepX97 toY := m.y + stepY98 err := proto.InputDispatchMouseEvent{99 Type: proto.InputDispatchMouseEventTypeMouseMoved,100 X: toX,101 Y: toY,102 Button: button,103 Buttons: buttons,104 Modifiers: m.page.Keyboard.getModifiers(),105 }.Call(m.page)106 if err != nil {107 return err108 }109 // to make sure set only when call is successful110 m.x = toX111 m.y = toY112 if m.page.browser.trace {113 if !m.updateMouseTracer() {114 m.initMouseTracer()115 m.updateMouseTracer()116 }117 }118 }119 return nil120}121// Scroll the relative offset with specified steps122func (m *Mouse) Scroll(offsetX, offsetY float64, steps int) error {123 m.Lock()124 defer m.Unlock()125 defer m.page.tryTrace(TraceTypeInput, fmt.Sprintf("scroll (%.2f, %.2f)", offsetX, offsetY))()126 m.page.browser.trySlowmotion()127 if steps < 1 {128 steps = 1129 }130 button, buttons := input.EncodeMouseButton(m.buttons)131 stepX := offsetX / float64(steps)132 stepY := offsetY / float64(steps)133 for i := 0; i < steps; i++ {134 err := proto.InputDispatchMouseEvent{135 Type: proto.InputDispatchMouseEventTypeMouseWheel,136 X: m.x,137 Y: m.y,138 Button: button,139 Buttons: buttons,140 Modifiers: m.page.Keyboard.getModifiers(),141 DeltaX: stepX,142 DeltaY: stepY,143 }.Call(m.page)144 if err != nil {145 return err146 }147 }148 return nil149}150// Down holds the button down151func (m *Mouse) Down(button proto.InputMouseButton, clicks int) error {152 m.Lock()153 defer m.Unlock()154 toButtons := append(m.buttons, button)155 _, buttons := input.EncodeMouseButton(toButtons)156 err := proto.InputDispatchMouseEvent{157 Type: proto.InputDispatchMouseEventTypeMousePressed,158 Button: button,159 Buttons: buttons,160 ClickCount: clicks,161 Modifiers: m.page.Keyboard.getModifiers(),162 X: m.x,163 Y: m.y,164 }.Call(m.page)165 if err != nil {166 return err167 }168 m.buttons = toButtons169 return nil170}171// Up releases the button172func (m *Mouse) Up(button proto.InputMouseButton, clicks int) error {173 m.Lock()174 defer m.Unlock()175 toButtons := []proto.InputMouseButton{}176 for _, btn := range m.buttons {177 if btn == button {178 continue179 }180 toButtons = append(toButtons, btn)181 }182 _, buttons := input.EncodeMouseButton(toButtons)183 err := proto.InputDispatchMouseEvent{184 Type: proto.InputDispatchMouseEventTypeMouseReleased,185 Button: button,186 Buttons: buttons,187 ClickCount: clicks,188 Modifiers: m.page.Keyboard.getModifiers(),189 X: m.x,190 Y: m.y,191 }.Call(m.page)192 if err != nil {193 return err194 }195 m.buttons = toButtons196 return nil197}198// Click the button. It's the combination of Mouse.Down and Mouse.Up199func (m *Mouse) Click(button proto.InputMouseButton) error {200 m.page.browser.trySlowmotion()201 err := m.Down(button, 1)202 if err != nil {203 return err204 }205 return m.Up(button, 1)206}207// Touch presents a touch device, such as a hand with fingers, each finger is a proto.InputTouchPoint.208// Touch events is stateless, we use the struct here only as a namespace to make the API style unified.209type Touch struct {210 page *Page211}212// Start a touch action213func (t *Touch) Start(points ...*proto.InputTouchPoint) error {214 // TODO: https://crbug.com/613219215 _ = t.page.WaitRepaint()216 _ = t.page.WaitRepaint()217 return proto.InputDispatchTouchEvent{218 Type: proto.InputDispatchTouchEventTypeTouchStart,219 TouchPoints: points,220 Modifiers: t.page.Keyboard.getModifiers(),221 }.Call(t.page)222}223// Move touch points. Use the InputTouchPoint.ID (Touch.identifier) to track points.224// Doc: https://developer.mozilla.org/en-US/docs/Web/API/Touch_events225func (t *Touch) Move(points ...*proto.InputTouchPoint) error {226 return proto.InputDispatchTouchEvent{227 Type: proto.InputDispatchTouchEventTypeTouchMove,228 TouchPoints: points,229 Modifiers: t.page.Keyboard.getModifiers(),230 }.Call(t.page)231}232// End touch action233func (t *Touch) End() error {234 return proto.InputDispatchTouchEvent{235 Type: proto.InputDispatchTouchEventTypeTouchEnd,236 TouchPoints: []*proto.InputTouchPoint{},237 Modifiers: t.page.Keyboard.getModifiers(),238 }.Call(t.page)239}240// Cancel touch action241func (t *Touch) Cancel() error {242 return proto.InputDispatchTouchEvent{243 Type: proto.InputDispatchTouchEventTypeTouchCancel,244 TouchPoints: []*proto.InputTouchPoint{},245 Modifiers: t.page.Keyboard.getModifiers(),246 }.Call(t.page)247}248// Tap dispatches a touchstart and touchend event.249func (t *Touch) Tap(x, y float64) error {250 defer t.page.tryTrace(TraceTypeInput, "touch")()251 t.page.browser.trySlowmotion()252 p := &proto.InputTouchPoint{X: x, Y: y}253 err := t.Start(p)254 if err != nil {255 return err256 }257 return t.End()258}...
trySlowmotion
Using AI Code Generation
1import "fmt"2func main() {3rod := new(Rod)4rod.trySlowmotion()5}6import "fmt"7func main() {8rod := new(Rod)9rod.trySlowmotion()10}11import "fmt"12func main() {13rod := new(Rod)14rod.trySlowmotion()15}16import "fmt"17func main() {18rod := new(Rod)19rod.trySlowmotion()20}21import "fmt"22func main() {23rod := new(Rod)24rod.trySlowmotion()25}26import "fmt"27func main() {28rod := new(Rod)29rod.trySlowmotion()30}31import "fmt"32func main() {33rod := new(Rod)34rod.trySlowmotion()35}36import "fmt"37func main() {38rod := new(Rod)39rod.trySlowmotion()40}41import "fmt"42func main() {43rod := new(Rod)44rod.trySlowmotion()45}46import "fmt"47func main() {48rod := new(Rod)49rod.trySlowmotion()50}51import "fmt"52func main() {53rod := new(Rod)54rod.trySlowmotion()55}56import "fmt"57func main() {58rod := new(Rod)59rod.trySlowmotion()60}
trySlowmotion
Using AI Code Generation
1import (2func main() {3 browser := rod.New().MustConnect()4 fmt.Println(page.MustInfo().URL)5 browser.MustClose()6}
trySlowmotion
Using AI Code Generation
1import "fmt"2func main() {3 rod = new(Rod)4 rod.trySlowmotion()5}6import "fmt"7func main() {8 rod = new(Rod)9 rod.trySlowmotion()10}11import "fmt"12func main() {13 rod = new(Rod)14 rod.trySlowmotion()15}16import "fmt"17func main() {18 rod = new(Rod)19 rod.trySlowmotion()20}21import "fmt"22func main() {23 rod = new(Rod)24 rod.trySlowmotion()25}26import "fmt"27func main() {28 rod = new(Rod)29 rod.trySlowmotion()30}31import "fmt"32func main() {33 rod = new(Rod)34 rod.trySlowmotion()35}36import "fmt"37func main() {38 rod = new(Rod)39 rod.trySlowmotion()40}41import "fmt"42func main() {43 rod = new(Rod)44 rod.trySlowmotion()45}46import "fmt"47func main() {48 rod = new(Rod)49 rod.trySlowmotion()50}51import "fmt"
trySlowmotion
Using AI Code Generation
1import java.util.Scanner;2public class Main {3 public static void main(String[] args) {4 Scanner s = new Scanner(System.in);5 System.out.println("Enter the number of rods in the array");6 int n = s.nextInt();7 int[] rods = new int[n];8 for(int i = 0; i < n; i++){9 rods[i] = s.nextInt();10 }11 Rod rod = new Rod();12 System.out.println(rod.trySlowmotion(rods));13 }14}15import java.util.*;16public class Rod {17 public int trySlowmotion(int[] rods) {18 int n = rods.length;19 int[] dp = new int[n];20 Arrays.fill(dp, 1);21 for(int i = 1; i < n; i++){22 for(int j = 0; j < i; j++){23 if(rods[j] < rods[i]){24 dp[i] = Math.max(dp[i], dp[j] + 1);25 }26 }27 }28 int max = 0;29 for(int i = 0; i < n; i++){30 max = Math.max(max, dp[i]);31 }32 return max;33 }34}35Time Complexity: O(n^2)36Space Complexity: O(n)
trySlowmotion
Using AI Code Generation
1import (2type rod struct {3}4func (r rod) trySlowmotion() {5 fmt.Println("In trySlowmotion method of rod class")6}7func main() {8 r.trySlowmotion()9}10import (11type rod struct {12}13func (r rod) trySlowmotion() {14 fmt.Println("In trySlowmotion method of rod class")15}16func main() {17 r.trySlowmotion()18}19import (20type rod struct {21}22func (r rod) trySlowmotion() {23 fmt.Println("In trySlowmotion method of rod class")24}25func main() {26 r.trySlowmotion()27}28import (29type rod struct {30}31func (r rod) trySlowmotion() {32 fmt.Println("In trySlowmotion method of rod class")33}34func main() {35 r.trySlowmotion()36}37import (38type rod struct {39}40func (r rod) trySlowmotion() {41 fmt.Println("In trySlowmotion method of rod class")42}43func main() {44 r.trySlowmotion()45}46import (47type rod struct {
trySlowmotion
Using AI Code Generation
1import "fmt"2import "github.com/rod/rod"3func main() {4 fmt.Println(page.MustTitle())5}6import "fmt"7import "github.com/rod/rod"8func main() {9 fmt.Println(page.MustTitle())10}11import "fmt"12import "github.com/rod/rod"13func main() {14 fmt.Println(page.MustTitle())15}16import "fmt"17import "github.com/rod/rod"18func main() {19 fmt.Println(page.MustTitle())20}21import "fmt"22import "github.com/rod/rod"23func main() {24 fmt.Println(page.MustTitle())25}26import "fmt"27import "github.com/rod/rod"28func main() {29 fmt.Println(page.MustTitle())30}31import "fmt"32import "github.com/rod/rod"
trySlowmotion
Using AI Code Generation
1import java.io.*;2import java.util.*;3{4 int n;5 int rod_length[];6 int price[];7 int max_price[];8 int max_price_index[];9 void getdata()throws IOException10 {11 BufferedReader br=new BufferedReader(new InputStreamReader(System.in));12 System.out.println("Enter the number of rods");13 n=Integer.parseInt(br.readLine());14 rod_length=new int[n];15 price=new int[n];16 max_price=new int[n];17 max_price_index=new int[n];18 System.out.println("Enter the rod length and price");19 for(int i=0;i<n;i++)20 {21 rod_length[i]=Integer.parseInt(br.readLine());22 price[i]=Integer.parseInt(br.readLine());23 }24 }25 void trySlowmotion()throws IOException26 {27 BufferedReader br=new BufferedReader(new InputStreamReader(System.in));28 System.out.println("Enter the length of rod");29 int rod=Integer.parseInt(br.readLine());30 int i,j;31 for(i=0;i<n;i++)32 {33 if(rod_length[i]<=rod)34 {35 max_price[i]=price[i];36 max_price_index[i]=i;37 }38 {39 max_price[i]=-1;40 max_price_index[i]=-1;41 }42 }43 int max=0;44 int index=-1;45 for(i=0;i<n;i++)46 {47 if(max_price[i]>max)48 {49 max=max_price[i];50 index=i;51 }52 }53 System.out.println("The maximum price is "+max);54 System.out.println("The length of rod is "+rod_length[index]);55 System.out.println("The price is "+price[index]);56 }57}58{59 public static void main(String args[])throws IOException60 {61 rod r=new rod();62 r.getdata();63 r.trySlowmotion();64 }65}66Program to find maximum profit by cutting a rod (Set 2)67Program to find maximum profit by cutting a rod (Set 3
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!